Ramdom numbers

1. Senior Member
Join Date
Dec 1969
Posts
358

Ramdom numbers

How do I ramdomly generate 5 different numbers between a specified amount ( i.e 1-10) in less than 30 lines of code?<BR><BR>I&#039;ve used the randomize statement & loads of IF statements, but I need a more efficient method, can anyone help?<BR><BR>Much appreciated,<BR><BR> - Raven

2. keeper Guest

RE: Ramdom numbers

If you perform a search at 4guys you&#039;ll find an excellent article.

3. Senior Member
Join Date
Dec 1969
Posts
614

RE: Ramdom numbers

for x=1 to 5<BR> randomize<BR> intmyNumber = Int((10 - 1 + 1) * Rnd+ 1)<BR> document.write intmyNumber & " "<BR> next<BR>

4. Senior Member
Join Date
Dec 1969
Posts
614

sorry ..not all different Whoops <eop>

.

5. Senior Member
Join Date
Dec 1969
Posts
614

This may work (still 24 lines tho')

Â*<BR>dim myArray(5)<BR>for x=1 to 5 <BR> randomize <BR> KeepGoing=0<BR> do while (KeepGoing=0)<BR> tmp= Int((10 - 1 + 1) * Rnd+ 1) &#039;new random number<BR> if x=1 then &#039;always insert save number<BR> myArray(x)=tmp<BR> KeepGoing=1<BR> else<BR> FoundOne=0<BR> for y=1 to x-1<BR> if myArray(y)=tmp then &#039;stop looking and get another<BR> FoundOne=1<BR> exit for<BR> end if<BR> next<BR> if FoundOne=0 then &#039;nowt found in array, so add<BR> myArray(x)=tmp<BR> KeepGoing=1<BR> end if<BR> end if<BR> loop<BR>next <BR>

6. Soeren Sprogoe Guest

RE: Ramdom numbers

I use the following 17 line code to generate an array with 5 random numbers between 1 and 25:<BR><BR>Function Random(intLower, intUpper)<BR> Random = Int((intUpper - intLower + 1) * Rnd + intLower)<BR>End Function<BR><BR>&#039;Make array with 5 random numbers<BR>Randomize<BR>Redim arrDummy(4)<BR>bolDummy = False<BR>Do While Not bolDummy<BR> For i = 0 to 4<BR> arrDummy(i) = Random(1,25)<BR> Next<BR> <BR> bolDummy = True<BR> <BR> For x = 0 To 4<BR> For y = x To 4<BR> If Not x = y And arrDummy(x) = arrDummy(y) Then bolDummy = False<BR> Next<BR> Next<BR>Loop<BR><BR>/Soeren Sprogoe<BR>www.bluebook.dk

Posting Permissions

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