random-seed

rand() is consistent across multiple function calls

℡╲_俬逩灬. 提交于 2019-12-01 10:59:41
When I try to generate 2 random numbers from within a function call, I'm getting repeated results. However, the rand() function works fine in loops or whatnot provided they are outside of function calls. I'm guessing that it is because of the system time and that program is small. But I do not know how else to vary the seed. #include <stdio.h> #include <ctype.h> #include <string.h> int test1(){ int randX, randY; int range = 5; srand( time(NULL) ); randX = (rand() % range) + 1; randY = (rand() % 15 ) + 1; printf("X:%d Y:%d\n", randX, randY); } int main(){ test1(); test1(); test1(); test1();

How to seed the random number generator for scikit-learn?

六眼飞鱼酱① 提交于 2019-12-01 09:25:55
问题 I'm trying to write a unit test for some of my code that uses scikit-learn. However, my unit tests seem to be non-deterministic. AFAIK, the only places in my code where scikit-learn uses any randomness are in its LogisticRegression model and its train_test_split , so I have the following: RANDOM_SEED = 5 self.lr = LogisticRegression(random_state=RANDOM_SEED) X_train, X_test, y_train, test_labels = train_test_split(docs, labels, test_size=TEST_SET_PROPORTION, random_state=RANDOM_SEED) But this

rand() is consistent across multiple function calls

…衆ロ難τιáo~ 提交于 2019-12-01 08:50:33
问题 When I try to generate 2 random numbers from within a function call, I'm getting repeated results. However, the rand() function works fine in loops or whatnot provided they are outside of function calls. I'm guessing that it is because of the system time and that program is small. But I do not know how else to vary the seed. #include <stdio.h> #include <ctype.h> #include <string.h> int test1(){ int randX, randY; int range = 5; srand( time(NULL) ); randX = (rand() % range) + 1; randY = (rand()

Does one need to call srand() C function per thread or per process to seed the randomizer?

◇◆丶佛笑我妖孽 提交于 2019-12-01 03:21:36
The caption pretty much says it. PS. This is for C++ Windows program. dreamlax According to the MSDN documentation on srand() (assuming you are using Microsoft's C runtime library), the seed is thread-local, so you need to call srand() for each thread that is using rand() . Note that this may not be the case in other implementations. Quoting from MSDN: The srand function sets the starting point for generating a series of pseudorandom integers in the current thread. Even if the answer weren't platform specific I'd suggest you avoid srand() and use <random> instead. Not only does the C++11

Is there a way to generate a seed out of a sequence of numbers?

不羁岁月 提交于 2019-11-30 19:12:27
For example if java produces the pseudorandom sequence: 9 3 2 5 6 by using 23 as a seed, how can I do the inverse? i.e. getting 23 out of the sequence 9 3 2 5 6 . Or how do I assign a seed for a certain sequence? It is easy to do if there is a database - just assign a random key for the sequence INSERT INTO SEQUENCE_TABLE VALUES (RANDOM_KEY, SEQUENCE) However if I'm not permitted to use a database, Is there a formula to do such a thing? The point of random number generators is that this is impossible. SecureRandom is designed to be especially cryptographically strong , but generally speaking,

Best way to seed mt19937_64 for Monte Carlo simulations

我的未来我决定 提交于 2019-11-30 10:56:26
问题 I'm working on a program that runs Monte Carlo simulation; specifically, I'm using a Metropolis algorithm. The program needs to generate possibly billions of "random" numbers. I know that the Mersenne twister is very popular for Monte Carlo simulation, but I would like to make sure that I am seeding the generator in the best way possible. Currently I'm computing a 32-bit seed using the following method: mt19937_64 prng; //pseudo random number generator unsigned long seed; //store seed so that

How can I retrieve the current seed of NumPy's random number generator?

北慕城南 提交于 2019-11-30 01:44:41
The following imports NumPy and sets the seed. import numpy as np np.random.seed(42) However, I'm not interested in setting the seed but more in reading it. random.get_state() does not seem to contain the seed. The documentation doesn't show an obvious answer. How do I retrieve the current seed used by numpy.random , assuming I did not set it manually? I want to use the current seed to carry over for the next iteration of a process. The short answer is that you simply can't (at least not in general). The Mersenne Twister RNG used by numpy has 2 19937 -1 possible internal states, whereas a

Best way to seed mt19937_64 for Monte Carlo simulations

丶灬走出姿态 提交于 2019-11-29 22:17:03
I'm working on a program that runs Monte Carlo simulation; specifically, I'm using a Metropolis algorithm. The program needs to generate possibly billions of "random" numbers. I know that the Mersenne twister is very popular for Monte Carlo simulation, but I would like to make sure that I am seeding the generator in the best way possible. Currently I'm computing a 32-bit seed using the following method: mt19937_64 prng; //pseudo random number generator unsigned long seed; //store seed so that every run can follow the same sequence unsigned char seed_count; //to help keep seeds from repeating

Structuring a Keras project to achieve reproducible results in GPU

久未见 提交于 2019-11-29 16:46:35
I am writing a tensorflow.Keras wrapper to perform ML experiments. I need my framework to be able to perform an experiment as specified in a configuration yaml file and run in parallel in a GPU. Then I need a guarantee that if I ran the experiment again I would get if not the exact same results something reasonably close. To try to ensure this, my training script contains these lines at the beginning, following the guidelines in the official documentation : # Set up random seeds random.seed(seed) np.random.seed(seed) tf.set_random_seed(seed) This has proven to not be enough. I ran the same

C++ need a good technique for seeding rand() that does not use time()

↘锁芯ラ 提交于 2019-11-29 10:44:56
I have a bash script that starts many client processes. These are AI game players that I'm using to test a game with many players, on the order of 400 connections. The problem I'm having is that the AI player uses srand( time(nullptr) ); But if all the players start at approximately the same time, they will frequently receive the same time() value, which will mean that they are all on the same rand() sequence. Part of the testing process is to ensure that if lots of clients try to connect at approximately the same time, the server can handle it. I had considered using something like srand(