Random Numbers from 0 to &HFFFFFFFF
Hi all
i want generate hex random values from 0 to &HFFFFFFFF
but it gives me error overflow 6
RandomNumberLong = CLng((Upperbound - Lowerbound + 1) * Rnd + Lowerbound)
what should i do to achieve this because i am getting overflow
are there any fix for this. or i should use GetTickCount.
Re: Random Numbers from 0 to &HFFFFFFFF
Code:
'Add a CommandButton to a blank Form
Option Explicit
Private Type udtCurrency
Value As Currency
End Type
Private Type LARGE_INTEGER
LowPart As Long
HighPart As Long
End Type
Private Sub Command1_Click()
Dim LI As LARGE_INTEGER, udtCurr As udtCurrency
udtCurr.Value = 4294967294@ * Rnd / 10000
LSet LI = udtCurr
Command1.Caption = "&&&H" & Hex$(LI.LowPart)
End Sub
Private Sub Form_Load()
Randomize
End Sub
Re: Random Numbers from 0 to &HFFFFFFFF
This question seems to have been cross-posted to the universe.
Have you even tried this?
Code:
'Said to be flawed prior to XP SP3:
Private Declare Function RtlGenRandom Lib "AdvAPI32" Alias "SystemFunction036" ( _
ByVal pRandomBuffer As Long, _
ByVal RandomBufferLength As Long) As Long
Private Function RGRLong() As Long
RtlGenRandom VarPtr(RGRLong), LenB(RGRLong)
End Function
Re: Random Numbers from 0 to &HFFFFFFFF
How many random numbers do you want to generate and are any matched numbers allowed in the list? Please advise and read dilettante's post. This thread seems rather suspicious.
Re: Random Numbers from 0 to &HFFFFFFFF
It's aslo possible that I've missed the point.
Other places where this question was posed there is additional discussion suggesting that unsigned arithmetic needs to be performed on the random value after it is obtained. Without a broader description of the problem I'm not sure how much help we can offer.
Re: Random Numbers from 0 to &HFFFFFFFF
Yet another way is by randomly toggling the sign bit after generating the lower 31 bits:
Code:
Option Explicit
Private Sub Command1_Click()
Dim RndNum As Long
RndNum = CLng(Int(2147483648@ * Rnd)) Or (&H80000000 * (1000! * (Timer - Fix(Timer)) Mod 2!))
Command1.Caption = "&&&H" & Hex$(RndNum)
End Sub
Private Sub Form_Load()
Randomize
End Sub
This one relies on the CryptGenRandom function:
Code:
Option Explicit
Private Declare Function CryptAcquireContextW Lib "advapi32.dll" (ByRef phProv As Long, ByVal pszContainer As Long, ByVal pszProvider As Long, ByVal dwProvType As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptGenRandom Lib "advapi32.dll" (ByVal hProv As Long, ByVal dwLen As Long, ByRef pbBuffer As Any) As Long
Private Declare Function CryptReleaseContext Lib "advapi32.dll" (ByVal hProv As Long, Optional ByVal dwFlags As Long) As Long
Public Function GenRnd() As Long
Const PROV_RSA_FULL = 1&, CRYPT_VERIFYCONTEXT = &HF0000000
Dim hProvider As Long
If CryptAcquireContextW(hProvider, 0&, 0&, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) Then
If CryptGenRandom(hProvider, 4&, GenRnd) = 0& Then GenRnd = 0&
hProvider = CryptReleaseContext(hProvider): Debug.Assert hProvider
End If
End Function