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
97
votes
7 answers

srand() — why call it only once?

This question is about a comment in this question Recommended way to initialize srand? The first comment says that srand() should be called only ONCE in an application. Why is it so?
Lipika Deka
  • 3,774
  • 6
  • 43
  • 56
78
votes
15 answers

Recommended way to initialize srand?

I need a 'good' way to initialize the pseudo-random number generator in C++. I've found an article that states: In order to generate random-like numbers, srand is usually initialized to some distinctive value, like those related with the…
Gary Richardson
  • 16,081
  • 10
  • 53
  • 48
46
votes
4 answers

Usefulness of `rand()` - or who should call `srand()`?

Background: I use rand(), std::rand(), std::random_shuffle() and other functions in my code for scientific calculations. To be able to reproduce my results, I always explicitly specify the random seed, and set it via srand(). That worked fine until…
Albert
  • 65,406
  • 61
  • 242
  • 386
40
votes
8 answers

Fill a vector with random numbers c++

I've got a vector that I'm trying to fill up with random numbers. I keep running into an issue however that the vector mostly outputs 0 each time that I'm running it (it shouldn't output a 0 ever). What am I doing wrong in my code written below to…
Valrok
  • 1,514
  • 7
  • 30
  • 49
26
votes
3 answers

implicit declaration of function 'time' [-Wimplicit-function-declaration]|

Whenever I try to use srand function I get this warning "implicit declaration of function 'time' [-Wimplicit-function-declaration]|" and a windows error report appears when running the compiled file, I'm a novice to c programming, I found this on…
Chathura Dodamgoda
  • 261
  • 1
  • 3
  • 3
20
votes
7 answers

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

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…
Flogo
  • 1,673
  • 4
  • 20
  • 33
19
votes
4 answers

How does srand relate to rand function?

I understand that rand() function generates the same number(s) each you run it if you don't change the seed number. That's where srand() comes in. Time is always changing so I know that you should pass the time(null) parameter to srand. My question…
Arrow
  • 691
  • 2
  • 7
  • 15
18
votes
1 answer

Is srand(time(NULL)) bad?

In rand() considered harmful it is pointed out that srand(time(NULL)) is bad because srand takes an unsigned int, but for Microsoft's compiler, time_t by default is a 64-bit number, therefore a narrowing conversion happens. However, time_t is…
user4111372
  • 217
  • 2
  • 3
17
votes
3 answers

Rand() % 14 only generates the values 6 or 13

Whenever I run the following program the returned values are always 6 or 13. #include #include #include #include using namespace std; //void randomLegs(); //void randomPush(); //void randomPull(); //void…
Kenneth
  • 343
  • 1
  • 2
  • 10
16
votes
7 answers

rand() generating the same number – even with srand(time(NULL)) in my main!

So, I'm trying to create a random vector (think geometry, not an expandable array), and every time I call my random vector function I get the same x value, though y and z are different. int main () { srand ( (unsigned)time(NULL)); …
Nick Sweet
  • 2,030
  • 3
  • 31
  • 48
15
votes
3 answers

Is it modern C++ to use srand to set random seed?

For code that uses std::random_shuffle, I need to set a random seed so that the pseudorandom sequences produced vary in each program run. The code example here makes a call to srand ( unsigned ( time (NULL) ) ); which needs to #include…
clstaudt
  • 21,436
  • 45
  • 156
  • 239
14
votes
2 answers

Implicit declaration of functions srand, rand and system

Trying to solve an exercise where I have to print a random temperature value between 35°C & -10°C every 5 seconds followed by the date and time. Everything looks to be working as intended, however when I enter the code in a test script I get…
TheAlPaca02
  • 485
  • 3
  • 8
  • 20
13
votes
10 answers

Random numbers in C

for(i = 0; i < n; i++){ srand(time(NULL)); printf("%d ", time(NULL)); for(j = 0; j < (n-1); j++){ a[i][j] = rand(); } } I try to generate random numbers, but they are the same... I try srand(i *…
Sergey Gavruk
  • 3,538
  • 2
  • 20
  • 31
13
votes
5 answers

How to use function srand() with time.h?

My program contains code that should generate a random positive integer number every time I execute it. It generates random numbers but only once. After that, when I execute same code, it gives me same values, and it is making my code useless. I…
user2273202
12
votes
4 answers

How often should I call srand() in a C++ application?

I have a C++ application which calls rand() in various places. Do I need to initialize srand() regularly to ensure that rand() is reasonably random, or is it enough to call it once when the app starts?
laurent
  • 88,262
  • 77
  • 290
  • 428
1
2 3
26 27