How to use random numbers in c #?

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.

+5
source share
6 answers

Use the Random class . For instance:

Random r = new Random();
int nextValue = r.Next(0, 100); // Returns a random number from 0-99
+14
source

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 .
  • . , . - , .
+7
Random rnd = new Random();
rnd.Next(minValue, maxValue);

.

rnd.Next(1,10);
+1

Next, min max :

var random = new Random();    
int randomNum = random.Next(min, max);
+1

Random, , , Random . 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

+1

 private static Random rnd = new Random(Environment.TickCount);

 private int RandomNum(int Lower, int Upper)
{

 return rnd.Next(Lower, Upper);//MyRandomNumber;

}
0

All Articles