|
-
Feb 16th, 2010, 04:47 PM
#1
Thread Starter
Member
how to make a random array not generate a number it has already generated
Hi
The thing is, im trying to make a random array of three numbers. Now, i've made the random array but i need it to not generate any numbers that it has already generated. for example:
1 2 3, not 1,1,2 or 1,2,1.
heres some code i made up to try and do it:
Code:
'generates three random numbers
Randomize
picdisplay1.Cls
picdisplay2.Cls
picdisplay3.Cls
lott_num(1) = Int((Rnd * 5) + 1)
lott_num(2) = Int((Rnd * 5) + 1)
lott_num(3) = Int((Rnd * 5) + 1)
If lott_num(1) = lott_num(2) Or lott_num(3) Then
lott_num(1) = Int((Rnd * 5) + 1)
Else
If lott_num(2) = lott_num(3) Then
lott_num(2) = Int((Rnd * 5) + 1)
End If
End If
picdisplay1.Print (lott_num(1))
picdisplay2.Print (lott_num(2))
picdisplay3.Print (lott_num(3))
if this is the right method and its some mistake ive made then i would appreciate it being pointed out, however, if there is a more elegant method, then im open to suggestions.
Thanks! 
Cal
-
Feb 16th, 2010, 04:49 PM
#2
Re: how to make a random array not generate a number it has already generated
Look at this FAQ topic on random numbers. Post 4 in that FAQ will interest you.
-
Feb 16th, 2010, 04:50 PM
#3
Thread Starter
Member
Re: how to make a random array not generate a number it has already generated
-
Feb 16th, 2010, 04:55 PM
#4
Re: how to make a random array not generate a number it has already generated
Cal
Before tackling your actual question, may I make
a suggestion regarding use of indenting....
The following is your original code with "better"
indenting. It surely helps me follow things, and
probably will help you and others, too.
Code:
'generates three random numbers
Randomize
picdisplay1.Cls
picdisplay2.Cls
picdisplay3.Cls
lott_num(1) = Int((Rnd * 5) + 1)
lott_num(2) = Int((Rnd * 5) + 1)
lott_num(3) = Int((Rnd * 5) + 1)
If lott_num(1) = lott_num(2) Or lott_num(3) Then
lott_num(1) = Int((Rnd * 5) + 1)
Else
If lott_num(2) = lott_num(3) Then
lott_num(2) = Int((Rnd * 5) + 1)
End If
End If
picdisplay1.Print (lott_num(1))
picdisplay2.Print (lott_num(2))
picdisplay3.Print (lott_num(3))
Call me finnicky, but it really does help 
Spoo
-
Feb 16th, 2010, 04:58 PM
#5
Thread Starter
Member
Re: how to make a random array not generate a number it has already generated
yeah, i usually do indent well, but im just testing code out quickly so havent bothered to do it this time.
Appreciate the thaught tho
-
Feb 16th, 2010, 04:59 PM
#6
Re: how to make a random array not generate a number it has already generated
Cal
Now, on to your actual question.
The problem with your current logic is that
you still leave yourself open to a "repeat"
Take your first branch ..
Code:
If lott_num(1) = lott_num(2) Or lott_num(3) Then
lott_num(1) = Int((Rnd * 5) + 1)
Theoretically, lott_num(1) could be a repeat and you'd
have no way to deal with it.
Seems to me that you need a loop. Essentially, you would
stay in the loop until you assure yourself that you do not
have a repeat, and at that point, you'd exit the loop.
Spoo
-
Feb 16th, 2010, 05:01 PM
#7
Re: how to make a random array not generate a number it has already generated
 Originally Posted by calarexar
yeah, i usually do indent well, but im just testing code out quickly so havent bothered to do it this time.
Appreciate the thaught tho 
Haha.. ok, just checking.
Spoo
-
Feb 16th, 2010, 05:01 PM
#8
Thread Starter
Member
Re: how to make a random array not generate a number it has already generated
thanks, il try using a boolean or something.
-
Feb 16th, 2010, 05:06 PM
#9
Thread Starter
Member
Re: how to make a random array not generate a number it has already generated
Spoo, hows this?
Code:
Do
If lott_num(1) = lott_num(2) Or lott_num(3) Then
found = True
lott_num(1) = Int((Rnd * 5) + 1)
Else
If lott_num(2) = lott_num(3) Then
found = True
lott_num(2) = Int((Rnd * 5) + 1)
Loop Until found = False
End If
End If
See! i do indent!
-
Feb 16th, 2010, 05:08 PM
#10
Thread Starter
Member
Re: how to make a random array not generate a number it has already generated
woops...just seen wats wrong wit dat...dear god im a noob at this if there ever was one!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|