|
-
Jul 12th, 2000, 11:09 PM
#1
Thread Starter
Fanatic Member
Here is the best code I could come up with to produce the numbers 0 through 3 in a random sequence with no repitition and equal probability for each possibility. The code has really been bugging me because I know there has to be a way that I can do this entirely with mathmatical boolean statements instead of using the For Next loops.
a = Int(Rnd * 4)
b = Int(Rnd * 3)
c = Int(Rnd * 2)
d = 0
b = (b + a + (-(b >= a))) - a 'boolean math, a and b are unique with equal probability
f = 0
g = c
For h = 0 To g 'random whether it's the first or second of the remaining two
For i = f To 3
If a <> i And b <> i Then c = i: i = 3: f = c + 1
Next i
Next h
For i = 0 To 3 'whatever's left goes to d, still equal probability
If a <> i And b <> i And c <> i Then d = i: i = 3
Next i
I was pretty stoked with the b= line where I was able to use the true false boolean value to make that variable distinct from the variable a, while giving it equal probability as to the numbers remaining. That's as far as I could get with doing it mathmatically instead of through loops.
If you're wondering why I'm submitting this problem for you all when the above code already works, it's partially because it's been bugging me, partially because I love tight code, but also because if the numbers are generated mathmatically, it will be alot more flexible and thus easier to get different lengths of random unrepeated numbers like a five or six digit string.
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
|