I am working on Pong in C # w / XNA.
I want to use a random number (within the range) to determine things like if the ball is picked up right or at an angle, and how fast the ball moves when it hits the oar.
I want to know how to implement it.
Use the Random class . For instance:
Random r = new Random(); int nextValue = r.Next(0, 100); // Returns a random number from 0-99
If you do not need cryptographically secure numbers, this Randomshould be good for you ... but there are two mistakes you need to know about:
Random
Random rnd = new Random(); rnd.Next(minValue, maxValue);
.
rnd.Next(1,10);
Next, min max :
var random = new Random(); int randomNum = random.Next(min, max);
Random, , , Random . RandomNumberGenerator, System.Security.Cryptography, .
RandomNumberGenerator
System.Security.Cryptography
:
RandomNumberGenerator rng = RandomNumberGenerator.Create(); byte[] rand = new byte[25]; //Set the length of this array to // the number of random numbers you want rng.GetBytes(rand);
: http://msdn.microsoft.com/en-us/library/system.security.cryptography.randomnumbergenerator(v=VS.80).aspx
private static Random rnd = new Random(Environment.TickCount); private int RandomNum(int Lower, int Upper) { return rnd.Next(Lower, Upper);//MyRandomNumber; }