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

Problems when calling srand(time(NULL)) inside rollDice function

When I used at first srand(time(NULL)) in rollDice() function it did not work. But when I put it in main, it works. Why is that? Can you tell me the logic? #include #include #include int rollDice(void) { return…
Lyrk
  • 1,936
  • 4
  • 26
  • 48
3
votes
4 answers

generate 1000 random card deck shuffles at once c++

I'm basically trying to build a computer simulation of a casino game idea that uses a standard deck of 52 cards. I would like to run 1000 games at once. I've used srand(time(NULL)) before, but I dont think it's possible to output 1000 different…
chuckieDub
  • 1,767
  • 9
  • 27
  • 46
3
votes
2 answers

Correctly seeding random number generator (Mersenne twister) c++

Besides being a rubbish programmer, my jargon is not up to scratch. I am going to try my best to explain myself. I have implemented a Merssene twister random number generator using randomlib. Admittedly I am not too familiar on how Visual 8 C++'s…
Seb
  • 3,655
  • 3
  • 17
  • 17
2
votes
3 answers

How to get the seed value from a random number generator in PHP

I would like to get the seed value after the rand() or mt_rand() is used. Essentially I want to store the seed so that I can use this seed to continue generating random numbers next time php is executed. I need a repeatable behavior for unit…
2
votes
2 answers

generate reliable pseudorandom number

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…
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
2
votes
3 answers

srand/rand slowly changing starting value

I have written a simple BASIC interpreter that I'm running on Clang/macOS. To implement RND(), desiring to use as standard C as possible, I used srand/rand. Near the start of the program (in retrobasic.c) I call srand (yes, once) with either null or…
Maury Markowitz
  • 9,082
  • 11
  • 46
  • 98
2
votes
2 answers

how to know the secret number from srand(time(0))

I had a network security class in my university. And there is a challenge for finding a secret number. Here is the code #include #include #include void init() { setbuf(stdin, NULL); setbuf(stdout, NULL); } int…
2
votes
2 answers

Is it allowed for srand(0) to have the same effect as srand(1)?

Is it allowed for srand(0) to have the same effect as srand(1)? C11, 7.22.2.2 The srand function (empahasis added): The srand function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to…
pmor
  • 5,392
  • 4
  • 17
  • 36
2
votes
2 answers

How would I call a function several times changing the random numbers inside the function every time it is called?

I am making a game for my C class (actually remaking one) and I have a function that produces random prices. The problem is that I need to call this function 60 times throughout the game and have the numbers regenerate to new ones every time the…
Neil
  • 43
  • 1
  • 5
2
votes
2 answers

Getting undeclared function and undefined symbol errors with rand_r when compiling a multithread C program on Windows

I am creating many threads and each one should output a random number. I know that srand() with rand is not thread-safe and indeed all the output numbers are the same. So I tried to use rand_r but I get the following error on my Windows terminal…
Anya Chan
  • 104
  • 1
  • 6
2
votes
1 answer

Order the odd numbers from an array in a descending order

I am making a code that will: print an array of random numbers order it to an ascending order find the odd numbers from that array arrange the odd numbers to a descending order. I've done the first three but I can't arrange the odd numbers to a…
Luxe
  • 69
  • 1
  • 6
2
votes
2 answers

C++ Problems generating new random numbers within loop/when prompted

I am almost finished with this basic dice program, but having trouble finding out what I did wrong for srand. It is supposed to randomly roll 2 dice for the user, and they have the option to keep what they rolled (K) or roll again (R). I would like…
amykp
  • 45
  • 12
2
votes
1 answer

Printing an an array of integers in random order without repeating in C++

I'm attempting to implement the printRandom() function which should repeatedly select and remove a random entry from the array C and then prints out the selected value until all values in the array C have been printed. They can only be printed one…
Alicia Sabatino
  • 57
  • 1
  • 2
  • 10
2
votes
1 answer

rand() with srand() is giving strangely similar results. The return from rand() is very similar

This is a seemingly common question, so I hope I don't sound redundant. But the range returned from rand() should be between 0 and RAND_MAX, however, when I do a very simple rand statement, I'm always getting returns within a very small range. This…
radiomime
  • 118
  • 1
  • 1
  • 11
2
votes
0 answers

srand() in constructor results in same numbers?

I have the following structure to randomly generate a field of integers: GameManager::GameManager(){ srand(time(NULL)); } void GameManager::newGame(int sizen, int sizem) { //ETC for(int i = 0; i < sizen; ++i){ …
lte__
  • 7,175
  • 25
  • 74
  • 131