ryszard.czerminski@pharma.novartis.com
2004-Jun-09 19:24 UTC
[R] how to initialize random seed properly ?
I want to start R processes on multiple processors from single shell
script
and I want all of them to have different random seeds.
One way of doing this is
sleep 2 # (with 'sleep 1' I am often getting the same number)
...
set.seed(unclass(Sys.time()))
Is there a simpler way without a need to sleep between invoking
different R processes ?
Ryszard
For reproducibility, you probably want to specify the starting seeds. I'd worry about using systime. (for selecting the seeds, you might consider random draws from a uniform). best, -tony ryszard.czerminski at pharma.novartis.com writes:> I want to start R processes on multiple processors from single shell > script > and I want all of them to have different random seeds. > One way of doing this is > > sleep 2 # (with 'sleep 1' I am often getting the same number) > ... > set.seed(unclass(Sys.time())) > > Is there a simpler way without a need to sleep between invoking > different R processes ? > > Ryszard > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- rossini at u.washington.edu http://www.analytics.washington.edu/ Biomedical and Health Informatics University of Washington Biostatistics, SCHARP/HVTN Fred Hutchinson Cancer Research Center UW (Tu/Th/F): 206-616-7630 FAX=206-543-3461 | Voicemail is unreliable FHCRC (M/W): 206-667-7025 FAX=206-667-4812 | use Email CONFIDENTIALITY NOTICE: This e-mail message and any attachme...{{dropped}}
> I want to start R processes on multiple processors from single shell > script > and I want all of them to have different random seeds.The usual way would be using the process ID for the initialization. Or the shell script could just hand out consecutive numbers 1, 2, 3, ... Lutz
ryszard.czerminski@pharma.novartis.com
2004-Jun-10 13:25 UTC
[R] how to initialize random seed properly ?
Here is my best solution so far using $RANDOM in bash
or maybe somebody has "pure R" solution ?
#!/bin/bash
for i in 1 2; do
p=tmp$i
cat > $p.R << EOF
set.seed($RANDOM)
cat('rnorm(3) =', rnorm(3), '\n')
EOF
nice R CMD BATCH --no-save --no-restore $p.R $p.log &
done
sleep 3
grep rnorm tmp[12].log
Best regards,
Ryszard
Ryszard Czerminski/PH/Novartis at PH
Sent by: r-help-bounces at stat.math.ethz.ch
06/09/2004 03:24 PM
To: r-help at stat.math.ethz.ch
cc:
Subject: [R] how to initialize random seed properly ?
I want to start R processes on multiple processors from single shell
script
and I want all of them to have different random seeds.
One way of doing this is
sleep 2 # (with 'sleep 1' I am often getting the same number)
...
set.seed(unclass(Sys.time()))
Is there a simpler way without a need to sleep between invoking
different R processes ?
Ryszard
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
perldoc -f srand has a quick and good advice on initializing a random sequence. I second the recommendation to prepare the list of random numbers in advance for sake of reproducibility. Itay -------------------------------------------------------------- itayf at fhcrc.org Fred Hutchinson Cancer Research Center