uhmm.. usaon pag gamit ug random integer numbers sa C#? pde ba ma return ang value para ma store ug variable?
pde post ninyo inyong sample code? hehehe
uhmm.. usaon pag gamit ug random integer numbers sa C#? pde ba ma return ang value para ma store ug variable?
pde post ninyo inyong sample code? hehehe
The following code returns a random number:
int num = random.Next();
The following code returns a random number less than 1000.
int num = random.Next(1000);
The following code returns a random number between min and max:
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
salamat bai deathnote.
but naa npud ko lain problem...
mao ni ako gi try nga code:
int a=0;
while(a <= 5)
{
int x = new Random().Next(1, 100);
Console.WriteLine(x);
a++;
}
sa 5 randoms, same ra ang output tanan sa iyang gi first random. i tot coincidence ra but ako gi try ka 10 times, mao man jud.
unsaon diay pag butang ang random sa loop?
patabang ko pls.![]()
you can use this:
or try to seed it.
int a=0;
Random r = new Random();
while (a <= 5)
{
int x = r.Next(1, 100);
Console.WriteLine(x);
a++;
}
int x = new Random().Next(1, 100);
yup the prob with ur code is that x should not be initialize as int but as random..and u initialize it outside the loop cause it would be redundant to do it over and over again...thas what the .Next is for.
Use merssienne twister algorithm if you intend to get a real random number if C#'s random doesnt cut it.
ah.. okies, ksabot na jud ko. thanks mga bossing.
Last edited by Gray_Fox; 09-30-2009 at 12:46 AM.
Similar Threads |
|