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?!

Read more of my web development posts on my website. Thank you.

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));

I'm afraid I no longer use this forum due to the new point allocation system.

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.

I'm afraid I no longer use this forum due to the new point allocation system.

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?

Read more of my web development posts on my website. Thank you.

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.

I'm afraid I no longer use this forum due to the new point allocation system.

Contributor

4010 Points

1926 Posts

## Random

Aug 01, 2013 06:13 PM|Afzaal.Ahmad.Zeeshan|LINK

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?!

All-Star

37441 Points

9076 Posts

## Re: Random

Aug 01, 2013 06:31 PM|AidyF|LINK

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

Contributor

4010 Points

1926 Posts

## Re: Random

Aug 02, 2013 05:52 AM|Afzaal.Ahmad.Zeeshan|LINK

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!?

All-Star

37441 Points

9076 Posts

## Re: Random

Aug 02, 2013 08:03 AM|AidyF|LINK

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.

Contributor

4010 Points

1926 Posts

## Re: Random

Aug 02, 2013 11:08 AM|Afzaal.Ahmad.Zeeshan|LINK

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?

All-Star

37441 Points

9076 Posts

## Re: Random

Aug 02, 2013 11:31 AM|AidyF|LINK

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.

Contributor

4010 Points

1926 Posts

## Re: Random

Aug 02, 2013 11:41 AM|Afzaal.Ahmad.Zeeshan|LINK

I understand term Random now! Thanks alot AidyF :)