Random Number
-
Hi I am new to cSharp and i would like to know how to generate a random number?? what code would i need to use?? I am aware of a method 'public virtual int Next();' However is there a easier way round this? Any help is much appreciated.
Cheers :)
-
Hi I am new to cSharp and i would like to know how to generate a random number?? what code would i need to use?? I am aware of a method 'public virtual int Next();' However is there a easier way round this? Any help is much appreciated.
Cheers :)
...you seem to be new to more than just C# ;) ..a simple search of "C# random" on the MSDN returns this at the top of their results..quite ironic I might add. I went to the MSDN to answer your question and Microsoft sent me back here lol. The solution provided in that article is probably overkill to your original question, but it is a great source for learning about random number generation
"I need build Skynet. Plz send code"
-
Hi I am new to cSharp and i would like to know how to generate a random number?? what code would i need to use?? I am aware of a method 'public virtual int Next();' However is there a easier way round this? Any help is much appreciated.
Cheers :)
Random rnd = new Random(); rnd.Next(); easier than what? :P Two lines is neither Hard nor Complex. I suppose you could make your own method that would allow you to turn your 2 lines into 1, but I don't think it'll save you much devlopment time! Random needs a seed and so needs to be constructed as you might want the same seed as a previous time or a new seed. You then need to get each random number so a method call is needed, which also has multiple overloads so you can specifiy the number range if you wish. If you want to remove all the flexability provided by the different parameters you can pass to the constructor and method then go ahead :) but as I said is it really worth it to save a single line of code? Thats assuming you only use it once of course! the more times you use rnd.Next() without creating a new instance of rnd the difference in total lines gets less and less.
-
Random rnd = new Random(); rnd.Next(); easier than what? :P Two lines is neither Hard nor Complex. I suppose you could make your own method that would allow you to turn your 2 lines into 1, but I don't think it'll save you much devlopment time! Random needs a seed and so needs to be constructed as you might want the same seed as a previous time or a new seed. You then need to get each random number so a method call is needed, which also has multiple overloads so you can specifiy the number range if you wish. If you want to remove all the flexability provided by the different parameters you can pass to the constructor and method then go ahead :) but as I said is it really worth it to save a single line of code? Thats assuming you only use it once of course! the more times you use rnd.Next() without creating a new instance of rnd the difference in total lines gets less and less.
Random rnd = new Random(); rnd.Next(); one line:)
-
Hi I am new to cSharp and i would like to know how to generate a random number?? what code would i need to use?? I am aware of a method 'public virtual int Next();' However is there a easier way round this? Any help is much appreciated.
Cheers :)
Extending off of originSH's post, you generally don't want to reseed the generator each time. THe seed is taken from the system clock and as a result if you do multiple calls back to back within a few dozen milliseconds while reseeding before each number generated you'll get the same return for all of them.
-- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
-
Random rnd = new Random(); rnd.Next(); one line:)