Miért generálja le ugyanazokat a számokat? (C#, Random)





Randomize.
Inicializálni kell használat előtt. Gondolom..





using System;
class Program
{
static void Main()
{
// 1
// Call method that uses class-level Random
F();
// 2
// Call same method
// The random number sequence still be random
F();
}
static Random _r = new Random();
static void F()
{
// Use class-level Random so that when this
// ... method is called many times, it still has
// ... good Randoms.
int n = _r.Next();
// If this declared a local Random, it would
// ... repeat itself.
Console.WriteLine(n);
}
}





1. Ezeket a számokat nem bekéred, hanem generálod. A kettő teljesen más dolog.
2. Egy Random kell, fölösleges a három.





Ne 3x hozd létre ugyan azt a Random objektumot ugyan azzal a seed-el és kérd le ugyan azt az első elemet..
1 Random objektumot hozz létre és annak kérd le az elemeit sorban:
Random rand = new Random();
int szam1 = rand.Next(255);
int szam2 = rand.Next(255);
int szam3 = rand.Next(255);
Amúgy pedig a feladat azt kéri hogy kérd be a felhasználótól a számokat.





Kapcsolódó kérdések:
Minden jog fenntartva © 2025, www.gyakorikerdesek.hu
GYIK | Szabályzat | Jogi nyilatkozat | Adatvédelem | Cookie beállítások | WebMinute Kft. | Facebook | Kapcsolat: info(kukac)gyakorikerdesek.hu
Ha kifogással szeretne élni valamely tartalommal kapcsolatban, kérjük jelezze e-mailes elérhetőségünkön!