Random generation.

J

Joergen Bondesen

Hi NG


I have maked this Code generator.


Option Explicit

Sub CodeGen()
Dim getcode As String
Dim x As Long

Open Application.ThisWorkbook.Path & "\" _
& Date & ".txt" For Output As #1

For x = 1 To 200
getcode = CodeGenerate(32, "1234567890ABCDEF")

'// Data To file
Print #1, getcode
Next x

Close #1
'Stop
End Sub


Function CodeGenerate(qty As Long, Str As String) As String
Dim x As Long
Dim y As Long
Dim Myvalue As String

For x = 1 To Len(Str)
Dim data() As Variant
ReDim Preserve data(1 To x)
data(x) = Mid(Str, x, 1)
Next x

'// Generate
For y = 1 To qty

'// Random ?????
Randomize Date * Time '* Rnd

'// Chr
Myvalue = data(Int((Len(Str) - 1 + 1) * Rnd + 1))

'// Make CodeString
If CodeGenerate = vbNullString Then
CodeGenerate = Myvalue
Else
CodeGenerate = CodeGenerate & Myvalue
End If
Next y
End Function



when I use only Date * Time:
'// Random ?????
Randomize Date * Time
I get blocks of 8 unique lines below each other, why?


when I use Date * Time * Rnd:
'// Random ?????
Randomize Date * Time * Rnd
then I get unique lines below each other, why?

How do I get optimal Randomize within Windows/Excel?

Can I find a sort of add-in for a "better" algorithm to my Random
generation?


Best Regards
Joergen Bondesen
 
D

Dana DeLouis

when I use only Date * Time:
Randomize Date * Time
I get blocks of 8 unique lines below each other, why?

My guess is that the value of Date * Time is "almost" constant during your
loop.
You get the same results if you called Randomize with a constant.
Try it with just Randomize 3

It appears that Randomize doesn't use the full 15 digits in its routine.
By including Rnd, you get values that are different.

Randomize Date * Time * Rnd
 
D

Dana DeLouis

getcode = CodeGenerate(32, "1234567890ABCDEF")

Hi. For Random Hex Codes, here's something a little different:

Function MakeHexCode(n As Long) As String
'- - - - - - - - - - - - - - - - - - -
'// n = Number of Characters [0-9 A-F]
'- - - - - - - - - - - - - - - - - - -

Dim FSO
Dim s As String
Set FSO = CreateObject("Scripting.FileSystemObject")

Do While Len(s) < n
s = s & Mid$(FSO.GetTempName, 4, 5)
Loop
MakeHexCode = Left$(s, n)
End Function
 
J

Joergen Bondesen

Hi Dana
It give the same probleme as Date * Time

but with your function I have generated 1000000 unique codes and that is
very good, thanks.

--
Best regards
Joergen Bondesen


Dana DeLouis said:
getcode = CodeGenerate(32, "1234567890ABCDEF")

Hi. For Random Hex Codes, here's something a little different:

Function MakeHexCode(n As Long) As String
'- - - - - - - - - - - - - - - - - - -
'// n = Number of Characters [0-9 A-F]
'- - - - - - - - - - - - - - - - - - -

Dim FSO
Dim s As String
Set FSO = CreateObject("Scripting.FileSystemObject")

Do While Len(s) < n
s = s & Mid$(FSO.GetTempName, 4, 5)
Loop
MakeHexCode = Left$(s, n)
End Function

--
HTH :>)
Dana DeLouis
Windows XP & Office 2003


Dana DeLouis said:
My guess is that the value of Date * Time is "almost" constant during
your loop.
You get the same results if you called Randomize with a constant.
Try it with just Randomize 3

It appears that Randomize doesn't use the full 15 digits in its routine.
By including Rnd, you get values that are different.

Randomize Date * Time * Rnd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top