|
-
May 1st, 2013, 12:57 PM
#1
Thread Starter
Lively Member
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.
Last edited by Kunical; May 1st, 2013 at 01:22 PM.
-
May 1st, 2013, 02:53 PM
#2
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
Last edited by Bonnie West; May 3rd, 2013 at 12:48 AM.
Reason: Removed Int()
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
May 2nd, 2013, 07:34 PM
#3
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
-
May 2nd, 2013, 07:52 PM
#4
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.
-
May 2nd, 2013, 08:14 PM
#5
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.
-
May 3rd, 2013, 02:35 AM
#6
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
Last edited by Bonnie West; May 3rd, 2013 at 04:00 AM.
Reason: Added GenRnd()
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|