# How to use Random Number Generator?

• ### Question

• User766369706 posted

Hi, I am trying to use Random number generator. However, the number generator keeps returning me 0. Is there an alternate way to random number or is there something wrong with my code in my transaction ID.

``` Random random = new Random();
int transactionID = random.Next(9999);```

Wednesday, July 27, 2016 10:47 AM

### Answers

• User669412048 posted

Hi Alvin

Use this:

```Random random = new Random();
int transactionID = random.Next(1, 9999);```

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Wednesday, July 27, 2016 11:19 AM
• User753101303 posted

Hi,

It shouldn't but it will return the same value if called in close succession. Try to create the random variable once. A common error is to get multiple numbers in close succession but the problem is that in this case you create a new sequence each time (which uses a time based seed) and then get the first number of the sequence which can cause the same number being returned multiple time.

Instead you shoudl ensure a single sequence is created so that you get the next number of the SAME sequence each time.

Another option could be to use a Guid (which from a practical point of view can really be considered unique, here you still have a risk to get two identical numbers one after the other).

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Thursday, July 28, 2016 9:59 AM

### All replies

• User669412048 posted

Hi Alvin

Use this:

```Random random = new Random();
int transactionID = random.Next(1, 9999);```

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Wednesday, July 27, 2016 11:19 AM
• User702547207 posted

You can refer to the below link which quotes lot of example.

http://stackoverflow.com/questions/2706500/how-do-i-generate-a-random-int-number-in-c

Wednesday, July 27, 2016 11:36 AM
• User-1377768212 posted

Hi,

You can refer this article :

http://www.dotnetperls.com/random

Thursday, July 28, 2016 9:54 AM
• User753101303 posted

Hi,

It shouldn't but it will return the same value if called in close succession. Try to create the random variable once. A common error is to get multiple numbers in close succession but the problem is that in this case you create a new sequence each time (which uses a time based seed) and then get the first number of the sequence which can cause the same number being returned multiple time.

Instead you shoudl ensure a single sequence is created so that you get the next number of the SAME sequence each time.

Another option could be to use a Guid (which from a practical point of view can really be considered unique, here you still have a risk to get two identical numbers one after the other).

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Thursday, July 28, 2016 9:59 AM
• User429228380 posted

Below link Helpful for you

http://www.c-sharpcorner.com/article/generating-random-number-and-string-in-C-Sharp/

Thursday, July 28, 2016 10:09 AM