A different type of Randomizer problem

Thread: A different type of Randomizer problem

1. Member
Join Date
Dec 1969
Posts
68

A different type of Randomizer problem

I am having a problem with the following code timing out horribly. It is to create a bingo board, with image 13 always in the middle.<BR><BR>I need to generate 24 unique random numbers from between 1-25 and not including 13.<BR><BR>&#060;TABLE WIDTH=600 BORDER=2 align="center" CELLPADDING=0 CELLSPACING=0 bordercolor="#000000"&#062;<BR><BR>&#060;%<BR>Slot s = 25<BR>Dim UsedNumberArray(24)<BR>UsedNumberArray(0) = 13<BR>ThatCount = 1<BR>For i = 1 to Slots<BR> If i &#060;&#062; 13 then<BR> NumberNew = False<BR> Do While NumberNew = False<BR> RandomNumber = Int( Slots * Rnd(1) ) + 1<BR> NumberNew = True<BR> For Each RandomNumber in UsedNumberArray<BR> NumberNew = False<BR> Next<BR> Loop<BR> <BR> UsedNumberArray(ThatCount) = RandomNumber<BR> ThatCount = ThatCount + 1<BR> <BR> If i = 1 or i = 6 or i = 11 or i = 16 or i = 21 then <BR> Response.Write("&#060;TR&#062;&#060;TD&#062;&#060; IMG SRC=&#039;images/bingo_"&RandomNumber&".jpg&#039; WIDTH=120 HEIGHT=125&#062;&#060;/TD&#062;")<BR> <BR> ElseIf i = 5 or i = 10 or i = 15 or i = 20 or i = 25 then<BR> Response.Write("&#060;TD&#062;&#060;IMG SRC=&#039;images/bingo_"&RandomNumber&".jpg&#039; WIDTH=120 HEIGHT=125&#062;&#060;/TD&#062;&#060;/TR&#062;")<BR><BR> Else<BR> Response.Write("&#060;TD&#062;&#060;IMG SRC=&#039;images/bingo_"&RandomNumber&".jpg&#039; WIDTH=120 HEIGHT=125&#062;&#060;/TD&#062;")<BR><BR> End If<BR><BR> ElseIf i = 13 then<BR> Response.Write("&#060;TD&#062;&#060;IMG SRC=&#039;images/bingo_13.jpg&#039; WIDTH=120 HEIGHT=125&#062;&#060;/TD&#062;")<BR><BR> End If<BR><BR>Next<BR>%&#062;<BR><BR>&#060;/TABLE&#062;

2. Senior Member
Join Date
Dec 1969
Posts
96,118

Not too hard

&#060;%<BR>Dim nums(25) &#039; 24 random numbers<BR>Dim used(25) &#039; used numbers...we just ignore 0<BR>used(13) = 13 &#039; mark 13 as already in use<BR>nums(13) = 13 &#039; and fix it in place in the random numbers<BR><BR>Randomize<BR>For n = 1 To 25<BR> If n &#060;&#062; 13 Then<BR> Do While True &#039; forever<BR> r = Int( Rnd * 24 ) + 1 &#039; get num from 1 to 25<BR> If used(r) &#060;&#062; r Then &#039; if not already used<BR> used(r) = r &#039; mark it as used<BR> nums(n) = r &#039; and collect it in the random num array<BR> Exit Do &#039; we found one!<BR> End If<BR> Loop &#039; only can get out via Exit Do above<BR> End If<BR>Next<BR><BR>&#039; and now you have a nice neat array of 25 random numbers <BR>&#039; from 1 to 25, but with 13 always in slot 13, in the array named "nums"<BR>&#039;<BR>For i = 1 To 25 &#039; all of them<BR> &#039; use it to generate your code<BR> If i MOD 5 = 1 Then Response.Write "&#060;TR&#062;"<BR> Response.Write "&#060;TD&#062;&#060;IMG SRC=""images/bingo_" & nums(i) & ".jpg"" WIDTH=120 HEIGHT=125&#062;&#060;/TD&#062;" <BR> If i MOD 5 = 0 Then Response.Write "&#060;/TR&#062;"<BR>Next<BR>%&#062;<BR>

3. Senior Member
Join Date
Dec 1969
Posts
96,118

****!

Couple of goofs in that...<BR><BR>&#060;% <BR>Dim nums(25) &#039; 25 random numbers <BR>Dim used(25) &#039; used numbers...we just ignore 0 <BR>used(13) = 13 &#039; mark 13 as already in use <BR>nums(13) = 13 &#039; and fix it in place in the random numbers <BR><BR>Randomize <BR>For n = 1 To 25 <BR> If n &#060;&#062; 13 Then <BR> Do While True &#039; forever <BR> r = Int( Rnd * 25 ) + 1 &#039; get num from 1 to 25 <BR> If used(r) &#060;&#062; r Then &#039; if not already used <BR> used(r) = r &#039; mark it as used <BR> nums(n) = r &#039; and collect it in the random num array <BR> Exit Do &#039; we found one! <BR> End If <BR> Loop &#039; only can get out via Exit Do above <BR> End If <BR>Next <BR><BR>&#039; and now you have a nice neat array of 25 random numbers <BR>&#039; from 1 to 25, but with 13 always in slot 13, in the array named "nums" <BR>&#039; <BR>For i = 1 To 25 &#039; all of them <BR> &#039; use it to generate your code <BR> If i MOD 5 = 1 Then Response.Write "&#060;TR&#062;" <BR> Response.Write "&#060;TD&#062;&#060;IMG SRC=""images/bingo_" & nums(i) & ".jpg"" WIDTH=120 HEIGHT=125&#062;&#060;/TD&#062;" <BR> If i MOD 5 = 0 Then Response.Write "&#060;/TR&#062;" <BR>Next <BR>%&#062; <BR>

4. Member
Join Date
Dec 1969
Posts
68

RE: Not too hard

Thanks for the assistance, but I&#039;m still timing out...

5. Member
Join Date
Dec 1969
Posts
68

RE: ****!

Thank you...working perfectly.<BR><BR>I&#039;m always trying too hard...

Posting Permissions

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