ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA randomize vs. srand (https://www.excelbanter.com/excel-programming/381330-vba-randomize-vs-srand.html)

[email protected]

VBA randomize vs. srand
 
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


NickHK

VBA randomize vs. srand
 
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

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





All times are GMT +1. The time now is 10:32 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com