|
-
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.
-
Jul 13th, 2000, 12:12 AM
#2
Hyperactive Member
Mike,
Are you looking for the fastest way to generate a 1-10 digit string of unique numeric values?
-
Jul 13th, 2000, 12:49 AM
#3
Thread Starter
Fanatic Member
Each digit unique, but capable of creating both four digits strings or five, with the range for each digit from 0 to 3 for the four digit string and 0 to 4 on the five digit string.
-
Jul 13th, 2000, 01:12 AM
#4
Hyperactive Member
You are going to kick yourself when you find out the answer ;-)
Code:
Function RandomString(Length as Integer) as String
Dim sTemp as String
Dim i as Integer
If Length > 0 and Length < 11 Then
sTemp = left("0123456789",Length)
Do While Len(sTemp) > 0
i = Int(Rnd * Len(sTemp))+1
RandomString = RandomString & mid(sTemp,i,1)
sTemp = left(sTemp,i-1) & mid(sTemp,i+1)
Loop
End If
End Function
Its fast and its furious ;-)
-
Jul 13th, 2000, 01:22 AM
#5
Thread Starter
Fanatic Member
Excuse my French, but that's ****ing beautiful! Thanks.
-
Jul 13th, 2000, 02:00 AM
#6
Member
This is not about this thread...... it is direct to GEN-X
I tried to have a look at your WEB today and the JAVA script fails.
It tells me it page http://gen-x.iweb.net.au/main.phtml cannot be displayed. Perhaps you want to check it out.
I tried to send you an e-mail about it last night but that got returned as non deliverable.
Are you undercover !!!!
-
Jul 13th, 2000, 01:08 PM
#7
Hyperactive Member
Bravo, Gen-X! Had I not needed to crawl back under a rock for some sleep I would have answered; you read my noodle mind where to go.
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
|