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
-5
votes
1 answer

C++ how to generate random weekly integer

I've created this code that generates a random integer value each day. So today the rand() will return 3 for example each time I call it, tomorrow it generates another value instead. std::vector myList = getMyList(); time_t t =…
Joseph
  • 1,029
  • 13
  • 26
-5
votes
2 answers

Can anyone explain why there is a 0 in srand(time(0)?

I have just begun to program with C++ and come across srand(time(0)). Can anyone explain why there is a 0 in srand(time(0))?
-6
votes
1 answer

What does " rand()%3-1 " mean in c++?

The program creates a two-dimensional array (named "table") and fills the array with random numbers (-1, 0 or 1). int main() { srand(time(0)); const int ROWS=3; const int COLS=4; int table[ROWS][COLS]; for (int i = 0; i <…
-7
votes
1 answer

C++ generate random numbers for a given seed

I need to create a random number generator that generates n random numbers for a given seed s. k largest or smallest values generated are also known. ex1 : Seed (s): 123 No of random numbers (n):100 largest 10 random numbers…
rami
  • 1
  • 3
-7
votes
2 answers

Why does my srand return all outcomes?

This is the beginning of a simple text game. In the game, you are supposed to go around finding dungeons and collecting artifacts. I use srand(time(0)) to do things such as find what stage to go to, attack, and what items you find, I haven' gotten…
-7
votes
5 answers

The meaning of srand()

I don't understand the meaning of srand() in to create a random number. Here is my code: /* srand example */ #include /* printf, NULL */ #include /* srand, rand */ #include /* time */ int main…
1 2 3
26
27