Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I wrote

<br />            Random RollOne = new Random();<br />            Random RollTwo = new Random();<br />                dieOne = RollOne.Next(1, 7);<br />                dieTwo = RollTwo.Next(1 , 7);<br />                sum = dieOne + dieTwo;<br />


but dieOne and dieTwo are always equal, how do you make two independent random values?

Edit:
Thanks Mikanu, it works now.

modified on Monday, July 13, 2009 10:43 PM
Posted

You should create one instance of Random as a static field of the class and use it whenever you need it.
It should never be a local variable of a method.
 
Share this answer
 
The correct way to do that is to create an instance of the Random class and then call one of the following methods:
- Next(Int32 max) // will return a random number smaller than max
- NextByte() // will return a random byte (between 0 and 255)
- NextDouble() // will return a random number between 0.0 and 1.0<br />

Here's a code sample which will generate two random numbers, a and b, between 0 and 10
<br />Random rnd = new Random();<br />int a = rnd.Next(10);<br />int b = rng.Next(10);<br />


 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900