Message Boards Message Boards

0
|
3230 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:

How can I properly use SeedRandom?

Posted 2 years ago

Hello everyone. I am working with some Monte Carlo simulations of a random variable. I want to use SeedRandom to store the results. My original code is as follows:

empiricPDF1 = SmoothKernelDistribution[RandomVariable1Histogram];
d1 = RandomVariate[empiricPDF1, 100];

I have changed this second line to

SeedRandom[1]; RandomVariate[empiricPDF1, {100}]

I however have some questions. First, I don't know how to get the data from SeedRandom. I have tried

d1 = SeedRandom[1];

and I get an error

"Part specification Null[[1]] is longer than depth of object"

I have also tried to use directly SeedRandom[1] further down the code obtaining the same error. Can someone please help me with this?

Any help is appreciated. Best regards. Jaime.

POSTED BY: Jaime de la Mota
3 Replies

I want to generate a sample of 100000 elements from empiricPDF1 and I want for them to be always the same values. I want to store them in a vector if possible.

POSTED BY: Jaime de la Mota
Posted 2 years ago
SeedRandom[123]; d1 = RandomVariate[NormalDistribution[], 100];
SeedRandom[123]; d2 = RandomVariate[NormalDistribution[], 100];

d1 == d2
(* True *)

Replace NormalDistribution and 100 based on what you need.

If you need to reuse the same numbers in a different session, save them and reload them when needed

fileName = FileNameJoin@{NotebookDirectory[], "random_variates.mx"};
DumpSave[fileName, d1];
ClearAll@d1

Get@fileName;
d1
(* Same 100 varieties *)
POSTED BY: Rohit Namjoshi
Posted 2 years ago

Hi Jaime,

What is RandomVariable1Histogram? SeedRandom is used to specify a seed value for the random number generator so that it generates a repeatable sequence of numbers.

If you evaluate the code below you will almost certainly get a different list of numbers.

RandomReal[1, 5]
(* {0.211826, 0.748657, 0.422851, 0.247495, 0.977172} *)

With the following, you will get the same list of numbers because the seed is the same

SeedRandom[123];
RandomReal[1, 5]
(* {0.455719, 0.977826, 0.943215, 0.962216, 0.302348} *)

If you want to generate repeatable results from your MC simulation then first evaluate SeedRandom with a specific seed value. To repeat with the same results, use SeedRandom with the same seed value. You can use BlockRandom if you want to scope the seed setting.

POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract