sooo i been googling a bit to find a inbuilt random number generators in visual studio 2010 C#, xna 4...found alot of stuff sooo ye found some stuff but from what i seen they use
Random rnd = new Random();
int random = rnd.Next(20);
ahhhhhhhhh anyone know the workaround for it or a link :P
omg you win! i saw that on some links about making its own method >.< but i didnt understand theirs.
yours is simple and to the point you win
and i suck for not being able to see it >.<
Cant you just declare the variable somewhere else and then use it to randomize ints where you want to? Like, say, this:
classProgram{// Declare the random number generator and init it in the constructorprivateRandomrnd;publicProgram(){rnd=newRandom();}// Print a given amount of random numberspublicvoidPrintRandomNumbers(intcount){for(intn=0;n<count;++n)Console.WriteLine(rnd.Next(0,100));}// Justs creates an instance and tell it to print 7 random numberspublicstaticvoidMain(string[]args){Programapp=newProgram();app.PrintRandomNumbers(7);Console.ReadLine();}}
You could of course just put it all in a separate method, like in the other answers. Just how I would maybe do it (was some time ago I programmed in C#, lol).
public int RandomNumber(int min, int max)
{
Random r = new Random();
return r.Next(min, max);
}
this worked for me
and calling it with
thisvar = RandomNumber(0, 416);
easy as
thanks again s3rius
Rollback Post to RevisionRollBack
To post a comment, please login or register a new account.
sooo i been googling a bit to find a inbuilt random number generators in visual studio 2010 C#, xna 4...found alot of stuff sooo ye found some stuff but from what i seen they use Random rnd = new Random(); int random = rnd.Next(20); ahhhhhhhhh anyone know the workaround for it or a link :P
Whatcha sayin' Willis?
omg you win! i saw that on some links about making its own method >.< but i didnt understand theirs.
yours is simple and to the point you win
and i suck for not being able to see it >.<
Uh, not quite that simple. You want something more like
Otherwise you'll be creating a new Random object every time you call this function. Which is, uh, bad...
@zeropoints: Go
Cant you just declare the variable somewhere else and then use it to randomize ints where you want to? Like, say, this:
You could of course just put it all in a separate method, like in the other answers. Just how I would maybe do it (was some time ago I programmed in C#, lol).
@RCIX: Go
I forgot. Is that possible in C#? It's valid in C+ + at least:
The static would make it only a single creation.
public int RandomNumber(int min, int max)
{
Random r = new Random();
return r.Next(min, max);
}
this worked for me
and calling it with
thisvar = RandomNumber(0, 416);
easy as
thanks again s3rius