|
-
Feb 5th, 2022, 12:57 AM
#1
Thread Starter
Member
Randomize the correct way?
hi, hv create a simple program to throw a dice when user press a button. am using Randomize + Rnd.
the question is - when do we use Randomize and should we repeat it or only once? as searching online some say only once before the rnd(inside form load) and some say before every rnd function.
what do you think and why?
thank you
-
Feb 5th, 2022, 02:54 AM
#2
Re: Randomize the correct way?
Only once.
Then you get a pseudo random sequence for every Rnd call.
Otherwise you initialize the random sequence again and again and get only the first number from each sequence
-
Feb 5th, 2022, 03:10 AM
#3
Thread Starter
Member
Re: Randomize the correct way?
 Originally Posted by Arnoutdv
Only once.
Then you get a pseudo random sequence for every Rnd call.
Otherwise you initialize the random sequence again and again and get only the first number from each sequence
ok. so its Once in the Form Load. Thanks
-
Feb 5th, 2022, 10:10 AM
#4
Re: Randomize the correct way?
 Originally Posted by aboka
the question is - when do we use Randomize and should we repeat it or only once?
When you think about how computers generate random numbers, just imagine a series of columns and rows where each row is a series of pre-defined random numbers that are selected in sequence every time you call something like Rnd. Randomize would analogous to selecting a specific row in the table of random numbers. If you do something like Randomize 22, think of it like you're selecting row 22 which will give you a specific sequence of numbers in the same order every time you select this specific row.
-
Feb 5th, 2022, 10:14 AM
#5
Thread Starter
Member
Re: Randomize the correct way?
 Originally Posted by Niya
When you think about how computers generate random numbers, just imagine a series of columns and rows where each row is a series of pre-defined random numbers that are selected in sequence every time you call something like Rnd. Randomize would analogous to selecting a specific row in the table of random numbers. If you do something like Randomize 22, think of it like you're selecting row 22 which will give you a specific sequence of numbers in the same order every time you select this specific row.
Thanks for the details and simple way to understand how they works
So if we set a fix number(Randomize 22), it will always be the same sequence
-
Feb 5th, 2022, 10:15 AM
#6
Re: Randomize the correct way?
 Originally Posted by aboka
So if we set a fix number(Randomize 22), it will always be the same sequence 
Exactly!
-
Feb 5th, 2022, 12:53 PM
#7
Re: Randomize the correct way?
If you don't provide a seed for Randomize, it uses the system timer, which is sort of a random number in itself. Typically, I execute a Randomize on startup, and never again ... and I don't provide a seed, just letting that be an extra random component.
--------
But, in another vein, if you want a "truly" random number (and not some algorithmically calculated number), you must do something like the following:
Code:
Option Explicit
Private Declare Function CryptAcquireContextW Lib "advapi32.dll" (hProv As Long, ByVal pszContainer As Long, ByVal pszProvider As Long, ByVal dwProvType As Long, ByVal dwFlags As Long) As Boolean
Private Declare Function CryptGenRandom Lib "advapi32.dll" (ByVal hProv As Long, ByVal dwlen As Long, pbBuffer As Any) As Boolean
Private Declare Function CryptReleaseContext Lib "advapi32.dll" (ByVal hProv As Long, ByVal dwFlags As Long) As Long
Private Declare Function GetMem4 Lib "msvbvm60" (ByRef Source As Any, ByRef Dest As Any) As Long ' Always ignore the returned value, it's useless.
Private Function RandomDecimal() As Variant
' This will return a random decimal number between 0 and 1, inclusive of 0 and exclusive of 1, with 64 bits (8 bytes) of precision.
Dim v1 As Variant
Dim v2 As Variant
Dim hCrypt As Long
Const PROV_RSA_FULL As Long = 1&
Const CRYPT_VERIFYCONTEXT As Long = &HF0000000
'
v1 = CDec(0) ' Create a Decimal number (zero).
Call CryptAcquireContextW(hCrypt, 0&, 0&, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) ' Initialize advapi32.
Call CryptGenRandom(hCrypt, 8&, ByVal PtrAdd(VarPtr(v1), 8&)) ' Get 8 bytes of random bits, and stuff into low order of Decimal.
Call CryptReleaseContext(hCrypt, 0&) ' Turn off advapi32.
'
v2 = CDec(0) ' Create another Decimal number (zero).
GetMem4 1&, ByVal PtrAdd(VarPtr(v2), 4&) ' Turn on low bit of high (third) byte of mantissa, making: &h100000000.
'
RandomDecimal = v1 / v2 ' Since v2 is 1 higher than v1 can ever be, the results will never reach ONE.
End Function
Public Function PtrAdd(ByVal Ptr As Long, ByVal iOffset As Long) As Long
' For adding (or subtracting) a small number from a pointer.
' Use PtrAddEx for adding (or subtracting) large numbers from a pointer.
PtrAdd = (Ptr Xor &H80000000) + iOffset Xor &H80000000
End Function
Private Sub Form_Load()
' Just a bit of a test.
Dim i As Long
For i = 1 To 20
Debug.Print RandomDecimal
Next
End Sub
The CryptGenRandom API function is only partially algorithmically based, and also partly based on real-world measures (such as mouse and keyboard input timings and other things), making it a "truly" random number.
Test output:
Code:
0.2786100541123288952061534673
0.3329793129613623347954655396
0.1849062813766881307477561724
0.6352340118723578440539795364
0.8912696375281999391242994391
0.118493287952525602107003988
0.4343108168072871824510680638
0.6807893403622565826763604613
0.9485711640799695990684467506
0.5306093503217231201630338377
0.7931983009752933834577044714
0.7518616773820660303473753516
0.1632423692486055201910852275
0.5447092905968399086059569814
0.0690070448307520092532350853
0.2779231923275289462384532446
0.1552264027743927280356302234
0.0440707741660403286808322942
0.1826613513177552144050794225
0.5579427629016347935510082567
-----
EDIT: Just as a further FYI, you could wrap the return of that RandomDecimal with CSng(RandomDecimal) and it'd basically be the same thing as Rnd, but more truly random.
Last edited by Elroy; Feb 5th, 2022 at 01:54 PM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Feb 5th, 2022, 01:11 PM
#8
Thread Starter
Member
Re: Randomize the correct way?
But, in another vein, if you want a "truly" random number (and not some algorithmically calculated number), you must do something like the following:
@Elroy hv read a bit on real rng but vb6 dont hv such a thing right? rgd your example, it looks cool and would be great if wanna generate some random serial number or something more complicated.
but is it possible(and better) to use it in my simple dice throwing or just selecting between a yes/no?
thanks,
-
Feb 5th, 2022, 01:17 PM
#9
Re: Randomize the correct way?
The Rnd function is absolutely fine for most games. I just wanted to point out that it is algorithmically based ... and, as such, by a hardcore hacker, it is crackable given enough numbers that it generates (i.e., they could predict all future numbers). My RandomDecimal function isn't crackable because "real-world" measures are incorporated into it.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Feb 5th, 2022, 01:24 PM
#10
Thread Starter
Member
Re: Randomize the correct way?
 Originally Posted by Elroy
The Rnd function is absolutely fine for most games. I just wanted to point out that it is algorithmically based ... and, as such, by a hardcore hacker, it is crackable given enough numbers that it generates (i.e., they could predict all future numbers). My RandomDecimal function isn't crackable because "real-world" measures are incorporated into it.
Thanks and will keep it as future reference. Think those encryption software(veracrypt etc) also using this as it need us to move our mouse randomly before creating the encrypt partition 
p/s - how do we gain Reputation? would like to give you star rating, but wouldn't allow me :-(
cheers,
-
Feb 5th, 2022, 01:29 PM
#11
Re: Randomize the correct way?
 Originally Posted by aboka
p/s - how do we gain Reputation? would like to give you star rating, but wouldn't allow me :-(
Hmmm, IDK why it wouldn't let you. I do know you have to spread it around and you can't just keep giving the same person more reputation.
But, no worries. I'm already maxed out on the green reputation blocks. But thanks.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Tags for this Thread
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
|