 ## 6 replies

Last post Aug 02, 2013 11:41 AM by Afzaal.Ahmad.Zeeshan

• ### Random

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

Read more of my web development posts on my website. Thank you.
• ### Re: Random

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;

// 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.
• ### Re: Random

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

Read more of my web development posts on my website. Thank you.
• ### Re: Random

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.
• ### Re: Random

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.