Results 1 to 26 of 26

Thread: max integer with rnd

  1. #1

    Thread Starter
    Banned learning c's Avatar
    Join Date
    Mar 2007
    Location
    canberra (australia's capital)
    Posts
    198

    max integer with rnd

    hello, anyone know what the max size integer that can be calculated with rnd statment.

    to elaborate:
    pseudo random integer = Int((High - Low + 1) * Rnd + Low)

    however i want to know that the max allowable number for High in the equation?

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: max integer with rnd

    Quote Originally Posted by learning c
    hello, anyone know what the max size integer that can be calculated with rnd statment.

    to elaborate:
    pseudo random integer = Int((High - Low + 1) * Rnd + Low)

    however i want to know that the max allowable number for High in the equation?
    The rnd returns a Single data type. But you're wrapping the code within the int() function so it would be an integer that has a max value of: 32767

  3. #3

    Thread Starter
    Banned learning c's Avatar
    Join Date
    Mar 2007
    Location
    canberra (australia's capital)
    Posts
    198

    Re: max integer with rnd

    :thanks:

  4. #4

    Thread Starter
    Banned learning c's Avatar
    Join Date
    Mar 2007
    Location
    canberra (australia's capital)
    Posts
    198

    Re: max integer with rnd

    ok what about just the rnd statment? it returns a single, however, it isn't a regular single is it as it returns numbers less than 1 and greater than 0.

    so what is the number of different unique permutations from rnd statment as well as max and min values?

  5. #5

    Thread Starter
    Banned learning c's Avatar
    Join Date
    Mar 2007
    Location
    canberra (australia's capital)
    Posts
    198

    Re: max integer with rnd

    ok so i tried it out in practice:

    Code:
    Private Sub Command1_Click()
    Dim i&, min, max, rand
    min = 1
    max = 0
    
    For i = 0 To 10000000
      
        rand = Rnd
        If rand > max Then max = rand
        If rand < min Then min = rand
        
    Next i
    
        Debug.Print min, max
    
    End Sub
    sometimes i get:
    0 0.9999997

    sometimes:
    3.576279E-07 0.9999999

    now that's an odd value for the min value as 3.576279E-07 = 0.0000003576279

    so now i am puzzled as to how to find out how many different unique numbers rnd creates

  6. #6
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: max integer with rnd

    It returns a single data type. The function itself just happens to not return negative values.

    I would assume the Min and max values are min and max of the single data type (–3.402823E+38 to 3.402823E+38).

    Maybe someone knows more about the rnd function than I do but I'm pretty sure that's right.

  7. #7

    Thread Starter
    Banned learning c's Avatar
    Join Date
    Mar 2007
    Location
    canberra (australia's capital)
    Posts
    198

    Re: max integer with rnd

    ok here are some more results never below E-07

    0 0.9999997
    3.576279E-07 0.9999999
    0 0.9999999
    1.788139E-07 0.9999999
    0 0.9999999
    0 0.9999997
    2.980232E-07 0.9999999
    0 0.9999999
    1.788139E-07 0.9999999
    0 0.9999999
    0 0.9999997
    2.980232E-07 0.9999999
    0 0.9999999
    1.788139E-07 0.9999999
    0 0.9999999
    0 0.9999997
    3.576279E-07 0.9999999

  8. #8
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: max integer with rnd

    I guess the rnd function returns numbers within that range. Also, try putting "Randomize" in the Form_Load() event and see if that changes anything.

  9. #9

    Thread Starter
    Banned learning c's Avatar
    Join Date
    Mar 2007
    Location
    canberra (australia's capital)
    Posts
    198

    Re: max integer with rnd

    Quote Originally Posted by DigiRev
    try putting "Randomize" in the Form_Load() event and see if that changes anything.
    good point, no difference

  10. #10
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: max integer with rnd

    Quote Originally Posted by DigiRev
    The rnd returns a Single data type. But you're wrapping the code within the int() function so it would be an integer that has a max value of: 32767
    You're confusing Int() with CInt(). Int() simply lops off the decimal value, while CInt() does a type conversion to integer.

    As far as I am aware, there is no upper bound for Int(). Try this in the immediate window:

    ?int(9999999999999999999)
    1E+19

    ?cint(9999999999999999999)
    [Run-time error '6': Overflow]

  11. #11
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: max integer with rnd

    Quote Originally Posted by learning c
    hello, anyone know what the max size integer that can be calculated with rnd statment.

    to elaborate:
    pseudo random integer = Int((High - Low + 1) * Rnd + Low)

    however i want to know that the max allowable number for High in the equation?
    Int() can handle double values, but you can only assign up to 32767 to integer variable on left side of equation... so for equation above your max is 32767.
    Last edited by leinad31; May 23rd, 2007 at 12:16 AM.

  12. #12
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: max integer with rnd

    As to the OP, I believe Milk when he said in this post:
    Quote Originally Posted by Milk
    hi, I think the seed is limited to 2 bytes, or at least if you use a larger number it only uses 2 bytes worth of that number. Rnd will repeat itself after 16777116 (2^24) calls. Any seeding you do just starts the sequence from a different place... which in effect can make it less random if you do it lots of times.
    That whole thread is a decent read, and links to this thread which has more discussion, which in turn links to the tutorial on using randomize.

    Hopefully something in one of those links will prove helpful.

  13. #13

    Thread Starter
    Banned learning c's Avatar
    Join Date
    Mar 2007
    Location
    canberra (australia's capital)
    Posts
    198

    Re: max integer with rnd

    hey Ellis Dee

    i need a pseudo random number at this stage, however i am trying to max the size of the pseudo number i get from rnd.

    pseudo random integer = Int((High - Low + 1) * Rnd + Low)

    as discussed only gets 16 bit pseudo random numbers, however, isn't a single 32bits? so rnd should be able to create 32bit numbers, i.e support a much higher max value :hmmm:

  14. #14
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: max integer with rnd

    What's limiting you is the use of an integer data type at the left side of the equation, this pseudo random integer.

  15. #15

    Thread Starter
    Banned learning c's Avatar
    Join Date
    Mar 2007
    Location
    canberra (australia's capital)
    Posts
    198

    Re: max integer with rnd

    Quote Originally Posted by leinad31
    What's limiting you is the use of an integer data type at the left side of the equation, this pseudo random integer.
    you're right :wanders off feeling stupid:

  16. #16

    Thread Starter
    Banned learning c's Avatar
    Join Date
    Mar 2007
    Location
    canberra (australia's capital)
    Posts
    198

    Re: max integer with rnd

    ok back to this again...

    so theorectically rnd is a single and should therefore be able to generate 2^32-1 different numbers...

    however when i try and extract them:
    Code:
    
    Private Sub Command1_Click()
    Dim rand, i&, max, min, maxval
    
    rand = CDec(0): maxval = CDec(4294967295#)
    max = CDec(0): min = CDec(0)
    For i = 0 To 1000000
        rand = Fix((maxval + 1) * Rnd)
        'Debug.Print rand
        If rand > max Then max = rand
        If rand < min Then min = rand
    Next i
    
    Debug.Print max, min
    
    End Sub
    
    Private Sub Form_Load()
    Randomize Timer
    End Sub
    


    i never get all 4294967295 integers :hmmmm:

    i think it has something to do with how many bytes are used for the floating point part of the number....

  17. #17
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: max integer with rnd

    1) There is a fixed sequence of random numbers (16,777,216 of them).
    2) The user enters RND(-x) and x is used to determine a start point for this fixed sequence
    3) Thereafter, RND returns numbers from this sequence, looping to the beginning as required.

    DIM Y as LONG (or DOUBLE) will yield those numbers

    Instead of RND(-x) which is useful for restoring saved games to allow playback, simple cryptography, etc., you usually see RANDOMIZE TIMER which is the equivalent of RND(-x) where x=some function of the timer.
    That sets essentially a random place for the fixed sequence to return values.

    Mac

    P.S. Here is a demonstration of the fixed sequence. The function MyRnd shows how random numbers are generated.

    True, RND does not return 0-16777215, but returns those numbers divided by 16777216. RND will return this to DOUBLE accuracy if you ask for it.

    Code:
    Option Explicit
    Dim Seed As Double
    Dim Temp As Double
    
    Private Sub Form_Load()
    Randomize Timer
    Seed = Rnd * 16777216
    Temp = MyRnd
    End Sub
    
    Private Function MyRnd() As Double
    MyRnd = Seed
    Temp = (16598013# * Seed + 12820163)
    Seed = Temp - Fix(Temp / 16777216) * 16777216
    End Function
    
    Private Sub Command1_Click()
    Text1.Text = Str$(Rnd * 16777216) + Space$(10) + Str$(MyRnd)
    End Sub
    Last edited by Mr.Mac; May 26th, 2007 at 05:51 AM.

  18. #18
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: max integer with rnd

    Int can handle even the maximum "integer" number possible in VB6.

    Code:
    MsgBox Int(CDec("79228162514264337593543950335"))
    Increment that by one and you get an error: and the error is raised by CDec function, because it goes off the range of the Decimal datatype. That is the maximum accuracy you can get for a number. Double lets you have bigger numbers than that, but it loses accuracy from the end (ie. you see zero instead of the correct numbers).

    You can run Rnd against that:
    Code:
        Randomize
        MsgBox Rnd * CDec("79228162514264337593543950335")
    And what about the datatype?

    Code:
    MsgBox VarType(Rnd * CDec("79228162514264337593543950335")) = vbDecimal
    Should return True: Rnd's result is coerced into Decimal.

    However, Decimal isn't an integer datatype really; I'm not fully sure what it is, because it doesn't behave fully like floating datatypes should behave. I guess it is a form of a hacked integer like Currency, but can have the decimal point set more freely.


    I also tested the result of running Rnd against Decimal datatype: the results held a lot of digits, which confirms Rnd's result was coerced into Decimal:
    Code:
        Dim decTest As Variant, decResult As Variant, lngA As Long, decMax As Variant
        Randomize
        decTest = CDec("79228162514264337593543950335")
        For lngA = 0 To 100000000
            decResult = Rnd * decTest
            If decResult > decMax Then decMax = decResult
        Next lngA
        MsgBox decTest & vbNewLine & decMax
    Warning! This runs for a long time, Variant is a slow datatype, not to forget math is far slower under IDE than compiled. As you can also see, it is very unlikely to hit the maximum number in a loop that loops far less than the amount of maximum number.

    One thing that comes with Rnd being coerced into Decimal is that the results don't often held any digits below the decimal point: and if they do, it is unlikely there is many of them:
    Code:
        Dim decTest As Variant, decResult As Variant, lngA As Long, lngCount As Long
        Randomize
        decTest = CDec("79228162514264337593543950335")
        For lngA = 0 To 10000
            decResult = Rnd * decTest
            If decResult <> Int(decResult) Then lngCount = lngCount + 1
        Next lngA
        MsgBox decTest & vbNewLine & lngCount & " of 10000 with digits below decimal point"
    I get only about a 1/10 ratio. Of course, this is due to the nature of Decimal in the maximum range of digits it can hold. Going for smaller numbers should give more numbers with digits under the decimal point, up to the point you get them almost every time.

  19. #19
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: max integer with rnd

    Great post, Merri.

  20. #20
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: max integer with rnd

    Quote Originally Posted by Merri
    Randomize
    MsgBox Rnd * CDec("79228162514264337593543950335")
    I would just like to ensure that nobody thinks that it is possible to generate 79228162514264337593543950335 random numbers.

    There is a fixed set of 16777216 "random numbers" that is always produced. The list contains 0/16777216 thru 16777215/16777216 in a certain fixed sequence per formula I suppied earlier here.

    RANDOMIZE determines where, in that fixed set, you will start getting numbers.

    This will generate all possible results of using the big number
    Code:
    dim zlist(0 to 16777215) as magic place to hold giant numbers
    dim i as long
    for i=0 to 16777215
      zlist(i)=(i*79228162514264337593543950335)/16777216
    next i
    The following will run forever
    Code:
    dim r as magic place to hold giant numbers
    do
      r=RND*79228162514264337593543950335
      for i=0 to 16777215
            if  r= zlist(i) then exit for
      next i
    loop while i<16777215 'i.e. we found a match
    Put another way: If all RND ever returned was 0,.25,.50,.75 then
    RND*1000 would only return 0, 250, 500, and 750. It would never return any other number such as 435.

    Sorry to bore you. Probably you all knew that anyway, but it looked like someone could think there can be a huge collection of random numbers. There is exactly 16777216 in BASICA, QBasic, VB, etc. No more, no less.

    Mac

  21. #21
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: max integer with rnd

    Mr. Mac: my point was mostly to answer the thread's first question, the maximum number you can use with it; not to see what are the limitations of Rnd function. Your sample code has a few errors, but that's not very important as you were merely stating the same you already said in your earlier post.

    However, to not let too many people down, it is possible get more randomness with custom randomizing function. I bothered to code something, not something that is very great but it should be good enough for far bigger accuracy than what you can get with only Rnd.

    Code:
    Public Function DecRnd() As Variant
        Dim dblResult As Double
        dblResult = CDbl(Rnd) + CDbl(Time) + (CDbl(Timer) - CDbl(Int(Timer)))
        DecRnd = (CDec(Rnd * 16777216) * CDec(GetTickCount And &H7FFFFF) * CDec(dblResult - Int(dblResult)) * CDec("1449551462400"))
    End Function
    I didn't bother to count the biggest number this gives out or how random it really is in the long run. It should make most people happy enough. I don't know in what kind of situatation someone would need so big random numbers (when coding with VB): but if someone had a real need for really random numbers big numbers, he'd have to look for more information on the subject.

  22. #22
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: max integer with rnd

    Quote Originally Posted by Merri
    far bigger accuracy ... I don't know in what kind of situatation someone would need so big random numbers
    The concept we are dealing with is the count of possible random numbers, not accuracy. RND is perfectly "accurate". It just doesn't have a huge number of possible outcomes.

    I couldn't analyze your algorithm to figure out the possibilities. I'm sure many.

    To answer your question about situation: If one is encrypting a message using RND, one needs more possible random numbers. I could supply a good algorithm, but I doubt anyone on this forum is interested.

    Mac

  23. #23
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: max integer with rnd

    I'm starting to believe there is some kind of difference between the Finnish and English word for accurate as this is what, fifth time somebody gets on that word once I've used it

  24. #24

    Thread Starter
    Banned learning c's Avatar
    Join Date
    Mar 2007
    Location
    canberra (australia's capital)
    Posts
    198

    Re: max integer with rnd

    thanks for your posts guys i think i was on the wrong path, i was trying to calculate one large 32bit+ pseudo random number and extract 3 or 4 btyes from it as an optimisation, however, i think that extracting the bytes from the 32bit number may take longer than getting 4 separate single bytes.

    but for interest's sake in case anyone else has to deal with it i found that you cannot reproduce the rnd sequence with vb6 code (from ms's website), so it is best to create your own algorithm based on sources in books or elsewhere on the net.

    i may come back to this if my app needs more optimising.

  25. #25
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: max integer with rnd

    Quote Originally Posted by learning c
    for interest's sake in case anyone else has to deal with it i found that you cannot reproduce the rnd sequence with vb6 code (from ms's website), so it is best to create your own algorithm based on sources in books or elsewhere on the net.
    What you say confuses me as the algorithm I showed exactly and perfectly reproduces the RND sequence (with VB5 code that I presume works in VB6).

    So you must be talking about something else.

    Mac

  26. #26
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: max integer with rnd

    Quote Originally Posted by Merri
    I'm starting to believe there is some kind of difference between the Finnish and English word for accurate as this is what, fifth time somebody gets on that word once I've used it
    Despite this being a rhetorical statement, I'll toss in my two cents:

    Accuracy is often mistakenly used when the concept being conveyed is precision. Accuracy is synonymous with correctness, while precision is synonymous with scale. "Accurate to 1 millimeter" tells us that a measurement is correct, and the precision of that measurement is 1 millimeter. Another way of looking at it would be those "margin of error: +/- 5%" qualifiers, which quantify accuracy but say nothing about precision.

    But in the specific example in this thread, the concept you were trying to convery was neither accuracy nor precision, but rather spread. As in, how many elements in the set of possible results.

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