05-01-2025, 08:17 PM
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...
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%