Results 1 to 21 of 21

Thread: Here's a better way to generate random nums than using Rnd

Threaded View

  1. #12

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,238

    Re: Here's a better way to generate random nums than using Rnd

    Quote Originally Posted by Steve Grant View Post
    Right here is my current code copied from post #1. It does not work. Your exe does not work either (a very brief flash of something and then nothing). Instead of keep saying I must be doing something wrong, why not look at the bigger picture. I am using Win10 Home 64bit, version 1607, build 14393.693. 8GB Ram, Athlon A10-7700K 4.2GHz. Is your system so very different?

    Code:
    Option Explicit
    Private Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" (ByRef phProv As Long, ByVal pszContainer As String, ByVal pszProvider As String, ByVal dwProvType As Long, ByVal dwFlags As Long) As Long
    Private Declare Function CryptReleaseContext Lib "advapi32.dll" (ByVal hProv 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 Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
    
    Dim hProv As Long
    Dim Quit As Boolean
    
    Private Sub Form_Load()
        Dim a As Long
        CryptAcquireContext hProv, vbNullString, vbNullString, 1, 0
        
        If hProv = 0 Then
            Unload Me
            Exit Sub
        End If
        
        Show
        Do Until Quit
            CryptGenRandom hProv, 4, a
            Cls
            Print a
            Sleep 100
            DoEvents
        Loop
    
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        Quit = True
        If hProv Then CryptReleaseContext hProv, 0
    End Sub
    As it is currently set with vbNullString for provider name, it uses the default provider for the current user. Depending on your current user permissions on your computer, that default could vary, so you may not be getting the same crypto provider (or possibly none at all) that I'm getting in my call to CryptAcquireContext. If the current default crypto provider does not support the use of CryptGenRandom, or if you are getting no crypto provider, then of course you can't make a call to CryptGenRandom.

    So I have a question for you. Are you using a "user" level Windows account or an "administrator" level Windows account? I'm logged in as an administrator account. If your account is only at a user level, then you may have a very underpowered default crypto provider or even no default crypto provider.
    Last edited by Ben321; Mar 13th, 2017 at 03:29 AM.

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