VBA randomize vs. srand

  • Thread starter Thread starter csugden
  • Start date Start date
C

csugden

Hi,

I'm trying to make a VBA version of a shuffling algorithm I've seen,
which is written (I think) in C. I've got the shape of it, I think, but
I think there is a problem due to the Randomize() seed function in vba
and the srand() function in C. Do they work the same way? How can I
replicate the behaviour of srand in vba?

Cheers,

Chris
 
Chris,
The VBA Help, explains the VBA psuedo random numbers.
From what I can tell about srand, this will replicate it, with the statement
"Rnd (-100)" ensuring that the same seed value gives the same number
sequence.
Comment out that and the same seed gives a different sequence, unlike srand.

Private Sub CommandButton1_Click()
Dim Runs As Long
Dim CellsToFill As Long

Const RunsMax As Long = 10
Const CellsMax As Long = 1000

Cells.ClearContents

For Runs = 1 To RunsMax
Rnd (-100)
Randomize (10)

For CellsToFill = 1 To CellsMax
Cells(CellsToFill, Runs).Value = Rnd()
Next
Next
End Sub

NickHK
 
Back
Top