Questions tagged [srand]

a function which initializes the pseudo-random number generator in C/C++ or PHP

A random seed is used to determine the initial state of a pseudo-random number generator. Such generators are used in many programming languages.

For a specific generator and a single value of seed the sequence of generated numbers will be the same.

Sample code:

#include <cstdlib>

int seed = 111;
int arr1[10];
int arr2[10];
srand (seed);
for (int a=0; a<10; a++) {
  arr1[a] = rand();
}
srand (seed);
for (int a=0; a<10; a++) {
  arr2[a] = rand();
}
for (int a=0; a<10; a++) {
  assert arr1[a] == arr2[a];
}
396 questions
11
votes
4 answers

Butterfly pattern appears in random walk using srand(), why?

About 3 years ago I coded a 2D random walk togheter with a coleague in C++, first it seemed to work properly as we obtained a diferent pattern each time. But whenever we decided to increase the number of steps above some threshold an apparent…
10
votes
2 answers

srand() scope in C++

When you call srand() inside of a function, does it only seed rand() inside of that function? Here is the function main where srand() is called. int main(){ srand(static_cast(time(0))); random_number(); } void…
Shadow
  • 351
  • 2
  • 5
  • 15
10
votes
2 answers

How can I store the state of the pseudo-random generator in Perl?

Is there a way to store the current state of the built in pseudo-random number generator in Perl so that when my program is run again, it can pick up the sequence from where it left off rather than starting with a new sequence? Right now, I am…
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
9
votes
5 answers

How does calling srand more than once affect the quality of randomness?

This comment, which states: srand(time(0)); I would put this line as the first line in main() instead if calling it multiple times (which will actually lead to less random numbers). ...and I've bolded the line which I'm having an issue with...…
user3920237
9
votes
5 answers

What is the best way to seed srand()?

The way I learned was to initially seed the random number generator with srand(time(NULL)) and then use calls to rand() to generate random numbers. The problem with this approach is if I run my program multiple times in the same second, the random…
Matt
  • 21,026
  • 18
  • 63
  • 115
9
votes
3 answers

random_shuffle not really random

I'm using the random_shuffle on a vector like this: #include vector 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…
Chin
  • 19,717
  • 37
  • 107
  • 164
8
votes
5 answers

What is the most correct way to generate random numbers in C with pthread

I have several threads running concurrently and each of them must generate random numbers. I want to understand if there is a pattern to follow, to understand if it is correct to initialize the random generator with srand in the main thread or if…
Raffo
  • 1,642
  • 6
  • 24
  • 41
8
votes
7 answers

How to get nth number in sequence of rand() directly without having to call rand() n times?

According to my understanding, setting srand with a particular seed causes the sequence of calls to rand() to produce the same series of numbers each time for that particular seed: Eg: srand(seed1); rand() // firstnumber…
Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190
8
votes
5 answers

If I don't specify srand(), what seed does rand() use?

Here is the code: #include #include #include int main(void) { int r; int i; for (i = 0; i < 100; i++) { r = rand() % 100 + 1; printf("%d\n", r); } return 0; } I've been trying…
milo64
  • 93
  • 1
  • 4
7
votes
4 answers

Find out what a random number generator was seeded with in C++

I've got an unmanaged c++ console application in which I'm using srand() and rand(). I don't need this to solve a particular problem, but was curious: is the original seed passed to srand() stored somewhere in memory that I can query? Is there any…
Brian
  • 5,826
  • 11
  • 60
  • 82
6
votes
4 answers

Issues with seeding a pseudo-random number generator more than once?

I've seen quite a few recommendations for not seeding pseudo-random number generators more than once per execution, but never accompanied by a thorough explanation. Of course, it is easy to see why the following (C/C++) example is not a good…
Flygsand
  • 115
  • 1
  • 8
6
votes
4 answers

What is the best practice when writing Perl tests that involve randomness?

While working on some updates to my module List::Gen, I decided to add a ->pick(num) method, which will return a num sized list of random elements from its source. To test this, I used srand to seed the random number generator, and made several…
Eric Strom
  • 39,821
  • 2
  • 80
  • 152
6
votes
5 answers

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

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…
Stéphane de Luca
  • 12,745
  • 9
  • 57
  • 95
6
votes
4 answers

Is it possible to increase the refresh speed of srand(time(NULL)) in C?

I am asking to see if there is a way of increasing the speed the srand(time(NULL)); function "refreshes"? I understand srand() produces a new seed according to time (so once a second), but I am looking for an alternative to srand() that can refresh…
6
votes
1 answer

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

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()); …
Dawei Yang
  • 606
  • 1
  • 9
  • 19
1
2
3
26 27