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