locked
Reseting a random number RRS feed

  • Question

  • Hey guys,

    I have a random number

    random = new Random();
    int randomNumber = random.Next(0, (cardDeck.Length + 1));

    and I tried to reinitialise it by doing this code below and the number won't change, is this correct and if not what can I do? Thanks in advance Damian. 

    randomNumber = random.next(0, (scopaDeck.Length + 1));

    Friday, October 12, 2012 7:50 AM

Answers

  • Hi,

    Not sure where new Random() is placed but a common issue is to place this in loop. As the default seed is time based, if this is called quickly you create the same sequence multiple times and you consume the first few values that are always the same.

    Placing this outside the loop allows to create a single random number sequence and consume each time one of its new unique value...

    Not sure what is your overall structure.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Friday, October 12, 2012 9:03 AM
  • Greetings Damian.

    Your loop above looks legit. What Patrice was getting at is that you don't want this line...

    Globals.random = new Random();

    ...to be in a loop. Ideally it should be called just once, at the start of runtime, and never again, or it could be called when a new game starts or the deck is shuffled. Something like that. Just don't put that line anywhere that it can be called repeatedly just microseconds apart.

    Monday, October 15, 2012 5:06 AM

All replies

  • Hi Damian,

    Random.Next(int32, int32) gets you a random number within a specified range. This can be identical to the previous random number, it might also be different. If your range is (too) narrow, the random number will be more likely to be the same it was earlier.

    For your information: the lower boundary is inclusive, the upper boundary exclusive.

    Hope this helps


    Please mark the best replies as answers - Twitter: @rickvdbosch - Blog: http://bloggingabout.net/blogs/rick

    Friday, October 12, 2012 8:18 AM
  • I have 40 numbers so I don't see the problem
    Friday, October 12, 2012 8:27 AM
  • Hi,

    Not sure where new Random() is placed but a common issue is to place this in loop. As the default seed is time based, if this is called quickly you create the same sequence multiple times and you consume the first few values that are always the same.

    Placing this outside the loop allows to create a single random number sequence and consume each time one of its new unique value...

    Not sure what is your overall structure.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Friday, October 12, 2012 9:03 AM
  • Thanks for that Patrick, my reinitialisation was in a loop; thanks I'll work with that. 

    Friday, October 12, 2012 9:09 AM
  • //Initialise that random number
                do
                    Globals.randomNumber = Globals.random.Next(0, (Globals.scopaDeck.Length + 1));
                    //If the random number is in the used list
    //Condition
                    while (Globals.usedRandomNumbers.Contains(Globals.usedRandomNumbers[Globals.randomNumber]));

    How do I move this around to get it out of a do while loop?
    Monday, October 15, 2012 3:13 AM
  • Greetings Damian.

    Your loop above looks legit. What Patrice was getting at is that you don't want this line...

    Globals.random = new Random();

    ...to be in a loop. Ideally it should be called just once, at the start of runtime, and never again, or it could be called when a new game starts or the deck is shuffled. Something like that. Just don't put that line anywhere that it can be called repeatedly just microseconds apart.

    Monday, October 15, 2012 5:06 AM
  • Oh right, thanks for that, it solves a massive problem in my game. 
    Monday, October 15, 2012 6:19 AM