Results 1 to 14 of 14

Thread: RealRand

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    RealRand

    I made this, because i thought i got better random numbers than ran, and dont say its not really random, because i know that

    VB Code:
    1. Private Declare Function GetTickCount& Lib "kernel32" ()
    2.  
    3. Private Sub Command1_Click()
    4.    Call RealRand(1, 5000) 'change to lowerbound,upperbound
    5. End Sub
    6.  
    7. Private Function RealRand(Lowb As Integer, upb As Integer)
    8. Randomize
    9. Dim t As Long
    10. Dim p As Integer
    11. t = GetTickCount
    12.    Do Until t >= Lowb And t <= upb
    13.    t = t / (Rnd * 100)
    14.    DoEvents
    15.      '  t = t / Int(upb - Lowb) * Rnd
    16.         If t < Lowb Then
    17.               t = upb * Rnd
    18.         End If
    19.    Loop
    20.                 MsgBox t
    21. End Function
    tell me if its better or worse or wahtever
    Last edited by |2eM!x; May 25th, 2005 at 10:46 PM.

  2. #2
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: RealRand

    Looks better to me

    I'll be using this one from now on.

    There is a test for randomness, in Liberty BASIC we would have a graphic box and then plot the points - the one that looked the most spread out was the most random
    Can you do a test like that in VB?

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  3. #3

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: RealRand

    hmm..is there a graph control in vb?

  4. #4

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: RealRand

    And heres another way, without API
    VB Code:
    1. Private Sub Command1_Click()
    2.    Call RealRand(1, 5) 'change to lowerbound,upperbound
    3. End Sub
    4.  
    5. Private Function RealRand(Lowb As Integer, upb As Integer)
    6. Randomize
    7. Dim t As String
    8. t = Replace(Time, "PM", vbNullString)
    9. t = Replace(t, "AM", vbNullString)
    10. t = Replace(t, ":", vbNullString)
    11. t = t * t * Rnd
    12.    Do Until t >= Lowb And t <= upb
    13.    t = t / (Rnd * 100)
    14.          DoEvents
    15.    If t < Lowb Then t = upb * Rnd
    16.    Loop
    17.                 MsgBox CInt(t)
    18. End Function
    Last edited by |2eM!x; May 26th, 2005 at 03:55 PM.

  5. #5
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: RealRand

    Quote Originally Posted by |2eM!x
    hmm..is there a graph control in vb?

    Nope, it was not a graph control, LB does not support controls in the way VB does

    It was basically like the picture box - Hang on I'll see if I can compile it and upload it to my website

    Edit: Here you go: http://allfreesoftware.helphousehost...nd_Example.zip

    Cheers,

    RyanJ
    Last edited by sciguyryan; May 26th, 2005 at 04:10 PM.
    My Blog.

    Ryan Jones.

  6. #6

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: RealRand

    i almost had it with mschart, but i have to go to work now so...not happening

  7. #7
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: RealRand

    I did a test, and I found that Rnd (the original is better)

    The test code:
    VB Code:
    1. Private Declare Function GetTickCount& Lib "kernel32" ()
    2.  
    3. Private Function RealRand(Lowb As Integer, upb As Integer) As Long
    4.     Dim T As Long
    5.     Dim P As Integer, Q As Single
    6.    
    7.     Randomize
    8.     T = GetTickCount
    9.    
    10.     Do Until T >= Lowb And T <= upb
    11.         Q = (Rnd * 100) + 1
    12.         T = T / Q
    13.         DoEvents
    14.         '  T = T / Int(upb - Lowb) * Rnd
    15.        
    16.         If T < Lowb Then
    17.             T = upb * Rnd
    18.         End If
    19.     Loop
    20.    
    21.     RealRand = T
    22. End Function
    23.  
    24. Private Sub Form_Load()
    25.     Dim Arr(500) As Byte, K As Long, Q As Long
    26.    
    27.     Me.ScaleMode = vbPixels
    28.    
    29.     Picture1.ScaleMode = vbPixels
    30.     Picture1.Width = 500
    31.     Picture1.Height = 125
    32.     Picture1.AutoRedraw = True
    33.    
    34.     Show
    35.     DoEvents
    36.    
    37.     Do
    38.         ' Get random value
    39.        
    40.         'K = RealRand(0, 500)
    41.         K = Fix(500 * Rnd)
    42.        
    43.         ' Increment color
    44.         Arr(K) = Arr(K) + 1
    45.        
    46.         ' draw line
    47.         Picture1.Line (K, 0)-(K, Picture1.ScaleHeight), RGB(Arr(K), Arr(K), Arr(K))
    48.        
    49.         ' Finish if reached 255
    50.         If Arr(K) = 255 Then Exit Do
    51.        
    52.         Q = Q + 1
    53.         If (Q Mod 10) = 0 Then Picture1.Refresh
    54.     Loop
    55.    
    56.     For K = 0 To 500
    57.         Picture1.Line (K, 0)-(K, Picture1.ScaleHeight), RGB(Arr(K), Arr(K), Arr(K))
    58.     Next K
    59.     Picture1.Refresh
    60.    
    61.     MsgBox "Done !"
    62. End Sub

    The black color means we never got that number, and white color it means we got that number many times.

    With ReadRnd:


    And with VB's Rnd:


    Seems that VB's Rnd is a lot more even...
    Attached Images Attached Images   

  8. #8

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: RealRand

    cool! ill work on a better one soon enough

  9. #9
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: RealRand

    Quote Originally Posted by |2eM!x
    cool! ill work on a better one soon enough
    I will work on a better one too but after work, cuz it will take some time to make it...

    To give you a hint on how i'll do it: You need to keep track of the random numbers you had (with a bolean array).
    Whenever you get a number, mark the item in the array as "True", and if it's already True, then find another Rnd number that is False.
    When all of them are True, reset them.

    I'm gonna make a class that will do this.

  10. #10

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: RealRand

    we've been doing some neat stuff in this codebank havent we

    i really like making these functions and junk!

    ill work on mine later tonight/tommorrow (got finals to take!)

  11. #11

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: RealRand

    VB Code:
    1. Option Explicit
    2. Private Declare Function GetTickCount& Lib "kernel32" ()
    3.  
    4. Private Sub Command1_Click()
    5. Call RealRand(1, 500)
    6. End Sub
    7.  
    8. Private Function RealRand(Lowerbound As Integer, Upperbound As Integer)
    9. Dim t As Long
    10.     t = GetTickCount * Rnd ^ 10 / 100
    11.         Do Until (t >= Lowerbound) And (t <= Upperbound)
    12.             t = t * Rnd
    13.             If t < Lowerbound Then t = t / Rnd: If t = 0 Then t = GetTickCount
    14.         Loop
    15.         MsgBox t
    16. End Function

    is that better? also, if you have an array with all the numbers used already, is that really random

  12. #12

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: RealRand

    VB Code:
    1. Private Declare Function GetTickCount& Lib "kernel32" ()
    2.  
    3. Private Function RealRand(Lowerbound As Integer, Upperbound As Integer)
    4. Dim t As Long
    5.     t = GetTickCount * Rnd ^ 10 / 100
    6.         Do Until (t >= Lowerbound) And (t <= Upperbound)
    7.             t = t * Rnd
    8.             If t < Lowerbound Then t = t / Rnd: If t = 0 Then t = GetTickCount
    9.         Loop
    10. Picture2.Line (t, 0)-(t, Picture2.ScaleHeight), RGB((t), (t), (t))
    11. Picture2.Refresh
    12. End Function
    13.  
    14. Private Sub Form_Load()
    15.     Dim Arr(500) As Byte, K As Long, Q As Long
    16.         Me.ScaleMode = vbPixels
    17.    
    18.     Picture1.ScaleMode = vbPixels
    19.     Picture1.Width = 500
    20.     Picture1.Height = 125
    21.     Picture1.AutoRedraw = True
    22.      Picture2.ScaleMode = vbPixels
    23.     Picture2.Width = 500
    24.     Picture2.Height = 125
    25.     Picture2.AutoRedraw = True
    26.         Show
    27.     DoEvents
    28.         Do
    29.         ' Get random value
    30.                 'K = RealRand(0, 500)
    31.         K = Fix(Rnd * 500)
    32.        
    33.         ' Increment color
    34.         Arr(K) = Arr(K) + 1
    35.        
    36.         ' draw line
    37.         Picture1.Line (K, 0)-(K, Picture1.ScaleHeight), RGB(Arr(K), Arr(K), Arr(K))
    38.        
    39.         ' Finish if reached 255
    40.         If Arr(K) = 255 Then Exit Do
    41.                 Q = Q + 1
    42.         If (Q Mod 10) = 0 Then Picture1.Refresh
    43.     Loop
    44.         For K = 0 To 500
    45.         Call RealRand(0, 500)
    46.     Next K
    47.     Picture1.Refresh
    48.    
    49.     MsgBox "Done !"
    50. End Sub
    im using that to draw the fancy pictures, and im getting closer

  13. #13
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: RealRand

    OK, here it is...

    The class (Also attached):
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetTickCount& Lib "kernel32" ()
    4.  
    5. Private RndArr() As Integer, RndMax As Long, LOVal As Long
    6. Private RndCount As Long, CurrStep As Byte, MaxStep As Integer
    7.  
    8. Public Sub Init(ByVal LOBound As Long, ByVal HIBound As Long, Optional ByVal MaximumStep As Integer = 1)
    9.     LOVal = LOBound
    10.     RndMax = HIBound - LOBound
    11.    
    12.     ReDim RndArr(RndMax - 1)
    13.     CurrStep = 1
    14.     MaxStep = MaximumStep
    15.    
    16.     If MaxStep <= 0 Then MaxStep = 1
    17.    
    18.     Randomize
    19.     Randomize GetTickCount * Rnd
    20. End Sub
    21.  
    22. Public Function GetNextRndVal() As Long
    23.     Dim RndVal As Long, K As Long
    24.    
    25.     Do
    26.         RndVal = Fix(RndMax * Rnd)
    27.     Loop Until RndArr(RndVal) < MaxStep
    28.    
    29.     RndArr(RndVal) = RndArr(RndVal) + 1
    30.     RndCount = RndCount + 1
    31.    
    32.     If RndCount >= RndMax * MaxStep Then
    33.         For K = 0 To UBound(RndArr)
    34.             RndArr(K) = 0
    35.         Next K
    36.        
    37.         RndCount = 0
    38.         CurrStep = 1
    39.     End If
    40.    
    41.     GetNextRndVal = LOVal + RndVal
    42. End Function

    And teh test:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim Arr(500) As Byte, K As Long, Q As Long
    5.     Dim EvenRnd As New clsEvenRnd
    6.    
    7.     EvenRnd.Init 0, 500, 1
    8.    
    9.     Me.ScaleMode = vbPixels
    10.    
    11.     Picture1.ScaleMode = vbPixels
    12.     Picture1.Width = 500
    13.     Picture1.Height = 125
    14.     Picture1.AutoRedraw = True
    15.    
    16.     Show
    17.     DoEvents
    18.    
    19.     Do
    20.         ' Get random value
    21.        
    22.         'K = RealRand(0, 500)
    23.         'K = Fix(500 * Rnd)
    24.         K = EvenRnd.GetNextRndVal
    25.        
    26.         ' Increment color
    27.         Arr(K) = Arr(K) + 1
    28.        
    29.         ' draw line
    30.         Picture1.Line (K, 0)-(K, Picture1.ScaleHeight), RGB(Arr(K), Arr(K), Arr(K))
    31.        
    32.         ' Finish if reached 255
    33.         If Arr(K) = 255 Then Exit Do
    34.        
    35.         Q = Q + 1
    36.         If (Q Mod 10) = 0 Then Picture1.Refresh
    37.     Loop
    38.    
    39.     For K = 0 To 500
    40.         Picture1.Line (K, 0)-(K, Picture1.ScaleHeight), RGB(Arr(K), Arr(K), Arr(K))
    41.     Next K
    42.     Picture1.Refresh
    43.    
    44.     MsgBox "Done !"
    45. End Sub
    The thing that makes a the bigest diference on numbers generated depends on the MaximumStep in the Init sub.

    For example when it's 1, like this:
    EvenRnd.Init 0, 500, 1

    It means that it will return random number between 0 and 500 and NONE of them repeat until all 500 are returned, then it repeates the whole process again.

    If it's like this (for eample):
    EvenRnd.Init 0, 500, 2
    It means that after the same nuber is returned 2 times it won't be returned again until after 1000 numbers are returned. For example you may get the same number 2 times in a row, but you won't get the numbers again until all other numbers are taken also (all others are returned 2 tiems also).

    The greater the MaximumStep is, the closer you get to the VB's Rnd

    I can't think of a better explanation to give, but if you try the code, and play with the numbers, I'm sure you will understand.

    Actually I think best way to understand it is to try with small ranges like:
    EvenRnd.Init 0, 4, 1
    Then try
    EvenRnd.Init 0, 4, 2
    Then
    EvenRnd.Init 0, 4, 3

    Return ~10 numbers for each

    And see the numbers you get, I'm sure you will understand it that way.
    Attached Files Attached Files

  14. #14

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: RealRand

    great job! i wish i could rate you, but alas your other projects have taken that spot up

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