"True" random numbers - VB6-VBForums
Results 1 to 7 of 7

Thread: "True" random numbers - VB6

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    11

    "True" random numbers - VB6

    I have put together a little function to generate numbers based on the system time (in ticks), current RAM usage, the actual VB random number generator, and an old trick (cant remember who made it) which said, to make random numbers, take a number, square it, and then take out the middle. This can go on and on forever. In VB's number generator, when I was generating random worlds for my game, I kept noticing a pattern in terrain generation. With this code, I had no such patterns. On to the code....
    Code:
    ' Code by ryzorg.info - 'rypped' on VBForums - Please include credit! It would mean a lot.
    Private Declare Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As MEMORYSTATUS)
    Dim oldrnd
    
    Private Type MEMORYSTATUS
           dwLength As Long
           dwMemoryLoad As Long
           dwTotalPhys As Long
           dwAvailPhys As Long
           dwTotalPageFile As Long
           dwAvailPageFile As Long
           dwTotalVirtual As Long
           dwAvailVirtual As Long
    End Type
    ' This part below can be done in a module.
    Private Sub Form_Load()
    Dim memuse As MEMORYSTATUS ' Create memory usage variable
    Call GlobalMemoryStatus(memuse) ' Write to that variable
    oldrnd = (timeGetTime * Rnd(65535) * memuse.dwAvailPhys) ' Make a random number, preparing the generator.
    End Sub
    
    Private Declare Function timeGetTime Lib "winmm.dll" () As Long
    Function dice(side)
    Dim memuse As MEMORYSTATUS ' Declare memory usage variable
    Call GlobalMemoryStatus(memuse) ' Write to that variable
    ramuse = memuse.dwAvailPhys ' Get the available physical memory
    Randomize (ramuse * Rnd(65535 + side)) ' Randomize VB's Rnd() based on RAM use
    rndnum = Mid(oldrnd ^ 0.5, 6, 4) ' Take the middle out of the old random number (generated in Form_Load)
    dice = Int(side * (rndnum / (10 ^ Len(rndnum)))) ' Finally, output to function.
    oldrnd = (timeGetTime * Rnd(65535) * memuse.dwAvailPhys) ' Save this generated number. It will be needed to generate the next.
    End Function
    I hope this helped somebody that needed better random numbers than what VB has to offer.

  2. #2
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: "True" random numbers - VB6

    My only question is why is oldrnd a Variant rather than a Double?
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    11,420

    Re: "True" random numbers - VB6

    Well I'm no expert and I'm not sure how to determine what "random enough" is, but there is also RtlGenRandom.
    Attached Files Attached Files

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    11

    Re: "True" random numbers - VB6

    My only question is why is oldrnd a Variant rather than a Double?
    I'm a lazy coder. I rarely ever define types for variables. Everything still works the same, however.
    Well I'm no expert and I'm not sure how to determine what "random enough" is, but there is also RtlGenRandom.
    Looks like a pretty good example. Thanks for sharing that. Did you put that together or was that something you found? It seems to relate a lot to the terrain generation that I've put together. To your example I am going to add my generator and see if things show patterns or not.

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    11,420

    Re: "True" random numbers - VB6

    Just something I threw together when you mentioned terrain generation. But I was really just after a quick way to illustrate distribution instead of using something like a bar graph... which now that I think about it might be a much better way to see how even the distribution is.

  6. #6
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,428

    Re: "True" random numbers - VB6

    Have you come across the Diehard and DieHarder tests for randomness?
    W o t . S i g

  7. #7
    Hyperactive Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    363

    Re: "True" random numbers - VB6

    Note that RtlGenRandom is not available on pre-XP Windows (I tried on Win2K and it failed) if that's important to you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.