locked
Random RRS feed

  • Question

  • User895691971 posted

    Hi: Here is a post of someone, where Mike posted an answer for this, http://forums.asp.net/t/1752838.aspx/1

    My Question is, will Random get same 'Random Number' results everytime, or will it give really Random ones, as its a software and they run on some algorithm. 

    What it will do? Suppose I have 5 number (Because we have 5 rows of data in the Bakery Site). Will it take out a number at the same algorithm, or will it take out any number at any time?!

    Thursday, August 1, 2013 6:13 PM

Answers

  • User-760709272 posted

    The trick with Random is when you re-use it and when to keep the same copy.  If you want a sequence of numbers in quick succession then creating a new instance of Random each time will likely give you the same numbers, as creating Random() uses a time-based starting seed so if you call it quick enough it gets the same seed, so you get the same numbers.

    Instead create it only once, and re-use the same instance of it.  If you are generating numbers in a repeater, keep the instance at class/page level so that the ItemDataBound event can access it.

    Here are both methods...look at the results to see the phenomenon in action

    int[] numbers = new int[10];
    
    // Get first 5 numbers using a new Random
    for (int i = 0; i < 5; i++)
    {
        Random different = new Random();
        numbers[i] = different.Next(100);
    }
    
    // Get second 5 numbers using the same Random
    Random same = new Random();
    
    for (int i = 5; i < 10; i++)
    {
        numbers[i] = same.Next(100);
    }
    
    System.Diagnostics.Debug.WriteLine(string.Join(", ", numbers));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 1, 2013 6:31 PM
  • User-760709272 posted

    The number in Random(100) is just the seed that the equation runs off, it has no relation to the first number, the maximum number or anything.  For simplicity, say Random gets random numbers by adding 10 to the seed.  If you do new Random(5) and get 5 numbers you'll get 15, 25, 35, 45, 55.  If you do new Random(20) you'll get 30, 40, 50, 60, 70.  Every time you use the same seed, you get the same numbers.  Obviously Random uses a better equation, as just adding 10 gives you rubbish random numbers, but it shows the point.

    Ticks is the number of 10/1,000,000 of a second since 01/01/0001 (or something like that). You can get the current tick by using DateTime.Now.Ticks.  As this number is constantly advancing, Random uses it as the seed if you don't supply one, so it is more likely you get new random numbers.  However if you create multiple Random objects within the same tick, they all generate the same sequence of random numbers.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 2, 2013 11:31 AM

All replies

  • User-760709272 posted

    The trick with Random is when you re-use it and when to keep the same copy.  If you want a sequence of numbers in quick succession then creating a new instance of Random each time will likely give you the same numbers, as creating Random() uses a time-based starting seed so if you call it quick enough it gets the same seed, so you get the same numbers.

    Instead create it only once, and re-use the same instance of it.  If you are generating numbers in a repeater, keep the instance at class/page level so that the ItemDataBound event can access it.

    Here are both methods...look at the results to see the phenomenon in action

    int[] numbers = new int[10];
    
    // Get first 5 numbers using a new Random
    for (int i = 0; i < 5; i++)
    {
        Random different = new Random();
        numbers[i] = different.Next(100);
    }
    
    // Get second 5 numbers using the same Random
    Random same = new Random();
    
    for (int i = 5; i < 10; i++)
    {
        numbers[i] = same.Next(100);
    }
    
    System.Diagnostics.Debug.WriteLine(string.Join(", ", numbers));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 1, 2013 6:31 PM
  • User895691971 posted

    You mean, it depends on time somehow too. As it retrieves a data and if suddenly we again extract Random number, the result will be same!?

    Friday, August 2, 2013 5:52 AM
  • User-760709272 posted

    Afzaal.Ahmad.Zeeshan

    You mean, it depends on time somehow too. As it retrieves a data and if suddenly we again extract Random number, the result will be same!?

    Random gets numbers that look random from an equation, but the equation needs a starting number.  Random sequences from the same number always yield the same results.  You can give it a starting number if you have one by passing it in the constructor

    new Random(myNumberVar)

    This number itself has to be somewhat random, if you hard-coded it you'd always get the same numbers.  If you just create with an empty constructor

    new Random()

    then the current time (ticks) is used as the starting number.  If your code creates multiple Random objects per tick they all get the same starting number so all return the same results.

     

    Friday, August 2, 2013 8:03 AM
  • User895691971 posted

    Oh, alot helpfull!

    But suppose, if I use 100 in the () brackets but there is no 100 in the result, what will happen? Will it still start the Random numbers that are close to 100 like 101 or something like that.

    Anyways I got the point very well!

    And what is current time("tick") ? I mean what is tick? 

    Friday, August 2, 2013 11:08 AM
  • User-760709272 posted

    The number in Random(100) is just the seed that the equation runs off, it has no relation to the first number, the maximum number or anything.  For simplicity, say Random gets random numbers by adding 10 to the seed.  If you do new Random(5) and get 5 numbers you'll get 15, 25, 35, 45, 55.  If you do new Random(20) you'll get 30, 40, 50, 60, 70.  Every time you use the same seed, you get the same numbers.  Obviously Random uses a better equation, as just adding 10 gives you rubbish random numbers, but it shows the point.

    Ticks is the number of 10/1,000,000 of a second since 01/01/0001 (or something like that). You can get the current tick by using DateTime.Now.Ticks.  As this number is constantly advancing, Random uses it as the seed if you don't supply one, so it is more likely you get new random numbers.  However if you create multiple Random objects within the same tick, they all generate the same sequence of random numbers.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 2, 2013 11:31 AM
  • User895691971 posted

    I understand term Random now! Thanks alot AidyF :)

    Friday, August 2, 2013 11:41 AM