Results 1 to 6 of 6

Thread: Random Numbers from 0 to &HFFFFFFFF

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Posts
    75

    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.

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    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)

  3. #3
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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

  4. #4
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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.
    Doctor Ed

  5. #5
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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.

  6. #6
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    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
  •  



Click Here to Expand Forum to Full Width