Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Random number generator C#

Options
  • 30-03-2009 11:34am
    #1
    Closed Accounts Posts: 16,095 ✭✭✭✭


    I'm new to using C#. Trying to create a maths game on a website at the moment. I need to get 2 random numbers generated but when the random number 1 is the same as random number 2. I need these to be different. Any idea what I can do?

    Thanks!


Comments

  • Registered Users Posts: 378 ✭✭sicruise


    while(random2 == random1){
        random2 = generateRandom();
    }
    


  • Closed Accounts Posts: 16,095 ✭✭✭✭omb0wyn5ehpij9


    I have that already, and it doesn't work. It still generates the same number twice :(


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    You have to seed the random number generator with a value (normally the current date/time).
    Although you should only seed once.

    I don't know C# so not sure if it will work (guess)

    Random r = new Random(DateTime.Now.Ticks);

    If you want state of the art random number generator this is the best out there.
    http://xkcd.com/221/


  • Closed Accounts Posts: 815 ✭✭✭KStaford


    Hobbes wrote: »
    You have to seed the random number generator with a value (normally the current date/time).
    Although you should only seed once.

    I don't know C# so not sure if it will work (guess)

    Random r = new Random(DateTime.Now.Ticks);

    If you want state of the art random number generator this is the best out there.
    http://xkcd.com/221/

    +1


  • Registered Users Posts: 197 ✭✭pauldiv


    The while test looks fine. I just tried it in php and it works.
    Paste up your code and let's have a look.


  • Advertisement
  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Quickly

    Random r = new Random();
    int r1 = r.Next();
    int r2 = r.Next();

    You can also seed the Random at the start by passing in an int32 value. Using the same seed will generate the same randoms (its pseudo random)

    MSDN
    By default, the parameterless constructor of the Random class uses the system clock to generate its seed value, while its parameterized constructor can take an Int32 value based on the number of ticks in the current time. However, because the clock has finite resolution, using the parameterless constructor to create different Random objects in close succession creates random number generators that produce identical sequences of random numbers.


  • Closed Accounts Posts: 16,095 ✭✭✭✭omb0wyn5ehpij9


    Thanks for the replies lads.....got it sorted before I checked back!


Advertisement