random numbers

1. Member
Join Date
Dec 1969
Posts
41

random numbers

Hi and thanks in advance for any help you can offer. I am still learning ASP and am having a problem producing a sequence of random numbers.<BR><BR>What I want to do is produce 4 random numbers on the same page - a, b, c, and d. Each number can only be 1, 2, 3, or 4 and none of the numbers can be the same. For example:<BR><BR>- 1,2,3,4<BR>- 4,1,2,3<BR>- 3,2,1,4<BR>- 2,4,3,1<BR><BR>I am able to produce one random number easily enough using the following code:<BR><BR>-----------------------------<BR>Function RandomNumber(intHighestNumber)<BR> Randomize<BR> RandomNumber = Int(intHighestNumber * Rnd) + 1<BR>End Function<BR><BR>Dim a(4)<BR><BR>a(1) = "1"<BR>a(2) = "2"<BR>a(3) = "3"<BR>a(4) = "4"<BR> <BR>image1 = a(RandomNumber(4))<BR>-----------------------------<BR><BR>I can even repeat the process three more times. What I haven&#039;t been able to do is make sure that all 4 numbers are different.<BR><BR>Sorry for the long-winded explanation and thanks again.<BR><BR>Chris Giesler

2. Senior Member
Join Date
Dec 1969
Posts
1,846

You'll need to loop the array each time you

add a value to it. Something like this:<BR><BR>Function RandomNumber(intHighestNumber)<BR> Randomize<BR> RandomNumber = Int(intHighestNumber * Rnd) + 1<BR>End Function<BR><BR>Dim x<BR>Dim z<BR>Dim a(4)<BR>Dim numloops<BR>Dim TempVar<BR>x=1<BR>Do Until x = 5 or numloops=1000<BR> numloops = numloops + 1<BR> TempVar = RandomNumber(4)<BR> &#039; Check to see if this is already in the array, if so flag Tempvar -1 so we know it was a dup<BR> For z = 1 to x<BR> If a(z) = TempVar Then TempVar = -1<BR> Next<BR> &#039; See if it was found, if so don&#039;t increment counter, if not add to the collection and bump counter<BR> If TempVar &#060;&#062; -1 Then<BR> a(x) = TempVar<BR> x=x+1<BR> End If<BR>Loop<BR><BR>This will loop up to 1000 times (this is to avert a possible infinite loop)

3. Member
Join Date
Dec 1969
Posts
41

RE: You'll need to loop the array each time y

Thanks peek. I really appreciate it.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•