Can you seed a value into Rnd?

User Name

Spammer
Joined
Aug 10, 2010
Messages
182
I'm using Rnd to generate random sequences. Out of 10 generated sequences only one of the sequences has GOOD Characteristics. It would be nice if I could save the seed value of this GOOD sequence and then tell Rnd what value to start on so that I could generate this sequence right off the bat and skip the other 9 runs. Is there a way to do this?
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
See Help for the Randomize statement.
 
Upvote 0
Here's where I'm confused. In the following code I'm expecting to see the same value for y2 as I do for y? This isn't happening. Can you tell me what I'm doing wrong?

z = Rnd
y = Rnd
Randomize z
y2 = Rnd
 
Upvote 0
For example,

Code:
Option Explicit
 
Dim fSeed           As Single
 
Sub FindOneILike()
    Dim i           As Long
    Dim avf(1 To 10) As Variant
    Dim sout        As String
 
    avf(1) = Rnd(-1)
 
    Do
        Rnd -1
        fSeed = avf(1)
        Randomize fSeed
        sout = vbNullString
        For i = 1 To 10
            avf(i) = Rnd
        Next i
    Loop Until MsgBox(Prompt:=Join(avf, vbLf) & vbLf & vbLf & "Like it?", _
                      Buttons:=vbYesNo) = vbYes
 
    MsgBox "Your seed is " & fSeed
    Debug.Print fSeed
End Sub
 
Sub UseIt()
    Dim i           As Long
    Dim avf(1 To 20) As Variant
    Dim f           As Single
    Rnd -1
    Randomize fSeed
    For i = 1 To 20
        avf(i) = Rnd
    Next i
    MsgBox Prompt:=Join(avf, vbLf)
End Sub

EDIT: You need to read the Help a lttle more carefully:

Note To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. Using Randomize with the same value for number does not repeat the previous sequence.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,763
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top