View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Dana DeLouis Dana DeLouis is offline
external usenet poster
 
Posts: 947
Default Random generation.

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" wrote in message
...
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

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


"Joergen Bondesen" wrote in message
...
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