## 6 replies

Last post Mar 21, 2008 03:17 PM by KyD

Member

93 Points

767 Posts

### 8 digit random number generation

Hi,

iam doing  asp.net project for a client project based on coupon generation ,my client

need random 8 digit number for each copon , please guide me.

Regards,
Kannandesikan

Star

10729 Points

2713 Posts

### Re: 8 digit random number generation

You can use the RND function, preceeded by the RANDOMIZE statement.

Member

390 Points

90 Posts

### Re: 8 digit random number generation

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rnd As New Random

Dim sb As New Text.StringBuilder
Dim x As Int32
For i As Int32 = 0 To 7
Randomize()
x = rnd.Next(0, 10)
sb.Append(x)
Next
Response.Write(sb.ToString)
End Sub

code for fun
----------------
Don't forget to mark it as Answer if it's useful for you

Member

390 Points

90 Posts

### Re: 8 digit random number generation

you also can consider to use GUID()

code for fun
----------------
Don't forget to mark it as Answer if it's useful for you

Member

30 Points

5 Posts

### Re: 8 digit random number generation

If your random number should not start with zero '0' in this scenario use the following code,

Random rnd=
new Random();

rnd.Next(10000000,99999999)

If the random number can have zero '0' in prefix, use the following code it will return 8 digit random number string you convert it to desired type....

Random rnd=new Random();

Star

13034 Points

1964 Posts

MVP

### Re: 8 digit random number generation

Mar 21, 2008 12:44 PM|Suprotim Agarwal|LINK

Hi,

How Random is Random() ?   :)

HTH,
Suprotim Agarwal

Participant

1033 Points

436 Posts

### Re: 8 digit random number generation

#### Suprotim Agarwal

I agree with Suprotim Agarwal.  Random is not alway random.   I would like to suggest that you check the coupon against the database before accepting the random number.  It is possible to get the same number or a predictable number during randomization.  You don't want a customer choosing 123456 as a coupon code even though it is a valid random number.

Basically, when I generate random, I would build a separate page allowing the user to generate the number.  As for the random number, I will use the datetimesecondmillisecond and concat it to your random number range.  Then I store the number into a database using a guid to reference the number.

I know it sounds like a lot of work but it really only takes 5 - 10 additional minutes.

That's my "2 cents" ... hope it helps.