srand

How to eliminate all sources of randomness so that program always gives identical answers?

大憨熊 提交于 2019-12-01 11:03:01
I have C++ code that relies heavily on sampling (using rand()), but I want it to be reproducible. So in the beginning, I initialize srand() with a random seed and print that seed out. I want others to be able to run the same code again but initializing srand() with that same seed and get exactly the same answer as I did. But under what circumstances is that guaranteed? I suppose that works only if the binaries are compiled with the same compiler on the same system? What are other factors that might make the answer differ from the one I got initially? The solution is to use the same code in all

How to eliminate all sources of randomness so that program always gives identical answers?

╄→гoц情女王★ 提交于 2019-12-01 09:06:24
问题 I have C++ code that relies heavily on sampling (using rand()), but I want it to be reproducible. So in the beginning, I initialize srand() with a random seed and print that seed out. I want others to be able to run the same code again but initializing srand() with that same seed and get exactly the same answer as I did. But under what circumstances is that guaranteed? I suppose that works only if the binaries are compiled with the same compiler on the same system? What are other factors that

C++ random int function

谁说我不能喝 提交于 2019-12-01 05:22:25
问题 Hello dear members of stackoverflow I've recently started learning C++, today I wrote a little game but my random function doesn't work properly. When I call my random function more than once it doesn't re-generate a number instead, it prints the same number over and over again. How can I solve this problem without using for loop? Thanks #include "stdafx.h" #include <iostream> #include <time.h> using namespace std; int rolld6(); int main() { cout<<rolld6()<<endl; cout<<rolld6()<<endl; system(

std::random_shuffle produce the same result even though srand(time(0)) called once

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 04:27:42
In a function, I want to generate a list of numbers in range: (This function will be called only once when executing the program.) void DataSet::finalize(double trainPercent, bool genValidData) { srand(time(0)); printf("%d\n", rand()); // indices = {0, 1, 2, 3, 4, ..., m_train.size()-1} vector<size_t> indices(m_train.size()); for (size_t i = 0; i < indices.size(); i++) indices[i] = i; random_shuffle(indices.begin(), indices.end()); // Output for (size_t i = 0; i < 10; i++) printf("%ld ", indices[i]); puts(""); } The results are like: 850577673 246 239 7 102 41 201 288 23 1 237 After a few

std::random_shuffle produce the same result even though srand(time(0)) called once

不羁岁月 提交于 2019-12-01 02:23:46
问题 In a function, I want to generate a list of numbers in range: (This function will be called only once when executing the program.) void DataSet::finalize(double trainPercent, bool genValidData) { srand(time(0)); printf("%d\n", rand()); // indices = {0, 1, 2, 3, 4, ..., m_train.size()-1} vector<size_t> indices(m_train.size()); for (size_t i = 0; i < indices.size(); i++) indices[i] = i; random_shuffle(indices.begin(), indices.end()); // Output for (size_t i = 0; i < 10; i++) printf("%ld ",

What‘s the difference between srand(1) and srand(0)

﹥>﹥吖頭↗ 提交于 2019-11-30 06:26:44
I just found out the hard way that srand(1) resets the PRNG of C(++) to the state before any call to srand (as defined in the reference ). However, the seed 0 seems to do the same, or the state before any call to srand seems to use the seed 0. What’s the difference between those two calls or what is the reason they do the same thing? For example this code ( execute on Ideone ) #include <stdio.h> #include <stdlib.h> int main() { for (int seed = 0; seed < 4; seed++ ) { printf( "Seed %d:", seed); srand( seed ); for(int i = 0; i < 5; i++ ) printf( " %10d", rand() ); printf( "\n"); } return 0; }

What is the equivalent of seeded random in Swift3 (Xcode8 beta 1)

你。 提交于 2019-11-30 05:54:22
问题 I need to start the same random number list over every execution of my app. srand/rand do not exist anymore. What should I do then? private extension Array { private func randomValues(_ seed: UInt32, num: Int) -> [Element] { srand (seed) var indices = [Int]() indices.reserveCapacity(num) let range = 0..<self.count for _ in 0..<num { var random = 0 repeat { random = randomNumberInRange(range) } while indices.contains(random) indices.append(random) } return indices.map { self[$0] } } 回答1: You

generate reliable pseudorandom number

匆匆过客 提交于 2019-11-29 15:59:50
I want to write a multiplayer game on iOS platform. The game relied on random numbers that generated dynamically in order to decide what happen next. But it is a multiplayer game so this "random number" should be the same for all device for every player in order to have a consistent game play. Therefor I need a good reliable pseudorandom number generator that if I seed it a same number first than it will keep generate same sequences of random number on all device (iPad/iPhone/iPodTouch) and all OS version. Looks like srand and rand will do the job for me but I am not sure does rand guarantee

random_shuffle not really random

半腔热情 提交于 2019-11-29 13:46:32
I'm using the random_shuffle on a vector like this: #include <algorithm> vector <Card> deck; //some code to add cards to the deck here random_shuffle ( deck.begin(), deck.end() ); When run, the content of the deck is mixed up, but this mixed-up order is kept when I restart the program. Did I miss something? How can I make it truly random? You need to seed the psuedo-random number generator first using srand . #include <algorithm> #include <cstdlib> ... std::srand(std::time(0)); vector <Card> deck; //some code to add cards to the deck here random_shuffle ( deck.begin(), deck.end() ); Note from

What‘s the difference between srand(1) and srand(0)

北战南征 提交于 2019-11-29 06:13:54
问题 I just found out the hard way that srand(1) resets the PRNG of C(++) to the state before any call to srand (as defined in the reference). However, the seed 0 seems to do the same, or the state before any call to srand seems to use the seed 0. What’s the difference between those two calls or what is the reason they do the same thing? For example this code (execute on Ideone) #include <stdio.h> #include <stdlib.h> int main() { for (int seed = 0; seed < 4; seed++ ) { printf( "Seed %d:", seed);