Results 1 to 7 of 7

Thread: Can anyone turn these loops into mathmatical boolean statements?

  1. #1

    Thread Starter
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526
    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.

    ~ mikeycorn

    With over 45,000 Questions in the User Submitted Database, it's the Most Popular Quiz Creation Software on the Net:

    PEST - The Personal Exam Self-Tester

  2. #2
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    Mike,

    Are you looking for the fastest way to generate a 1-10 digit string of unique numeric values?

  3. #3

    Thread Starter
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526
    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.

    ~ mikeycorn

    With over 45,000 Questions in the User Submitted Database, it's the Most Popular Quiz Creation Software on the Net:

    PEST - The Personal Exam Self-Tester

  4. #4
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    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 ;-)

  5. #5

    Thread Starter
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526
    Excuse my French, but that's ****ing beautiful! Thanks.
    ~ mikeycorn

    With over 45,000 Questions in the User Submitted Database, it's the Most Popular Quiz Creation Software on the Net:

    PEST - The Personal Exam Self-Tester

  6. #6
    Member
    Join Date
    Jun 2000
    Location
    Perth Western Australia
    Posts
    41

    Cool

    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 !!!!

  7. #7
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    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
  •  



Click Here to Expand Forum to Full Width