Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
generating a random number in the full range of that number? (Integer, Long)
#1
I'm trying to generate random numbers in the full range of a value, for example 
an integer can be -32,768 to 32,767 
and a long can be  -2,147,483,648 to 2,147,483,647. 
If I try those for the min, max values, it always returns the max negative value 
- even if I try changing the type of  NumSpread to a bigger type... 
I tried upto _Integer64 and it still doesn't work. 

Any idea how to get it to return a random value in the full range? 

Any help appreciated...

Code: (Select All)
' Type Name               Type suffix symbol   Minimum value                  Maximum value                Size in Bytes
' ---------------------   ------------------   ----------------------------   --------------------------   -------------
' INTEGER                 %                    -32,768                        32,767                       2
' _UNSIGNED INTEGER       ~%                   0                              65,535                       2
' LONG                    &                    -2,147,483,648                 2,147,483,647                4
' _UNSIGNED LONG          ~&                   0                              4,294,967,295                4
' _INTEGER64              &&                   -9,223,372,036,854,775,808     9,223,372,036,854,775,807    8
' _UNSIGNED _INTEGER64    ~&&                  0                              18,446,744,073,709,551,615   8

Dim inp$

Cls
Print "RandomInteger% (1, 10)"
For i% = 1 To 10
    Print _ToStr$(RandomInteger%(1, 10))
Next i%
Input "Press Enter to continue"; inp$

Cls
Print "RandomInteger% (-32768, 32768)"
For i% = 1 To 10
    Print _ToStr$(RandomInteger%(-32768, 32768))
Next i%
Input "Press Enter to continue"; inp$

Cls
Print "RandomLong& (1, 10)"
For i% = 1 To 10
    Print _ToStr$(RandomLong&(1, 10))
Next i%
Input "Press Enter to continue"; inp$

Cls
Print "RandomLong& (-2147483648, 2147483647)"
For i% = 1 To 10
    Print _ToStr$(RandomLong&(-2147483648, 2147483647))
Next i%
Input "Press Enter to continue"; inp$

System

Function RandomLong& (Min&, Max&)
    Dim NumSpread&&

    'NumSpread& = (Max& - Min&) + 1
    'RandomLong& = _Cast(Long, Rnd * NumSpread&) + Min&

    'NumSpread~& = (Max& - Min&) + 1
    'RandomLong& = _Cast(Long, Rnd * NumSpread~&) + Min&

    NumSpread&& = (Max& - Min&) + 1
    RandomLong& = _Cast(Long, Rnd * NumSpread&&) + Min&
End Function ' RandomLong&

Function RandomInteger% (Min%, Max%)
    Dim NumSpread%
    NumSpread% = (Max% - Min%) + 1
    RandomInteger% = Int(Rnd * NumSpread%) + Min% ' GET RANDOM # BETWEEN Max% AND Min%
End Function ' RandomInteger%
Reply
#2
For integer...

DIM NUM AS INTEGER
Num = INT(RND * 65536)

That's it.  Long would be the same, but with the bigger value of an Unsigned Long
Reply
#3
Thanks - it's amazing how simple that is. 

One thing that tripped me up was the INT function... 

I thought that was only for integer types, so I'd need to find the Long equivalent, so I tried CLNG, but no workie. 

I tried INT and it worked.

Code: (Select All)
Do
    Dim MyInt As Integer
    MyInt = Int(Rnd * 65536)
    Print "MyInt = Int(Rnd * 65536) = " + _ToStr$(MyInt)
    Input "Press Enter or Q to quit"; in$
    in$ = UCase$(Left$(_Trim$(in$), 1)): If in$ = "Q" Then Exit Do

    Dim MyLong As Long
    'MyLong = CLng(Rnd * 4294967295)
    MyLong = Int(Rnd * 4294967295)
    Print "MyLong = CLng(Rnd * 4294967295) = " + _ToStr$(MyLong)
    Input "Press Enter or Q to quit"; in$
    in$ = UCase$(Left$(_Trim$(in$), 1)): If in$ = "Q" Then Exit Do
Loop
System
Reply




Users browsing this thread: 2 Guest(s)