How to assign a random value to a variable?

    3 answers 3

    Using the System.Random : Next , NextDouble class . For example:

    Random random = new Random(); int r = random.Next(0, 100); 

    assigns r to a random value between 0 and 100.

    • one
      From 0 to 99 (upper limit - exclusively). - Raider

    For a better imitation of randomness, it suffices to make Random static:

     static Random random = new Random(); int r = random.Next(0, 100); 
       Random rn = new Random(DataTime.Now.Milisecond); int a = rn.Next(1, 100); 

      For the best imitation of chance I advise DataTime.