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

rand() return the same number

I am making a simple example in C with rand() but the function always return the same number despite i am using srand(). This is the code: #include #include #include int generate(int min, int max) { …
2
votes
1 answer

Why don't child processes produce the same random number if you don't set the seed?

In the docs for srand, it says: Another case is that you may want to call "srand" after a "fork" to avoid child processes sharing the same seed value as the parent (and consequently each other). I could have sworn I never ran into this, so I…
Chas. Owens
  • 64,182
  • 22
  • 135
  • 226
2
votes
1 answer

Why does srand(time(NULL)) gives me segmentation fault on main?

Need some help here. I want to understand what's happening in this code. I'm trying to generate random numbers as tickets to the TCP_t struct created inside ccreate function. The problem is, everytime I executed this code WITHOUT the…
2
votes
3 answers

C++ - Initializing a 2D array with pointer notation

I'm working on 2D array in C++. I'm having trouble connecting the concepts of arrays and pointers. I know that they are related in terms of memory allocation and accessing elements. For example int *arr; int num = arr + 1*sizeof(int); is the same…
Q_A
  • 433
  • 2
  • 6
  • 14
2
votes
2 answers

Unable to shuffle cards randomly

I am doing a project that shuffle 10 cards (12) time randomly but it didn't work with my code. #include #include #include void show(int[],int); void shuffle(int[],int,int); int main (void) { int…
heroo
  • 21
  • 4
2
votes
4 answers

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

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…
Frank
  • 64,140
  • 93
  • 237
  • 324
2
votes
5 answers

rand() not giving me a random number (even when srand() is used)

Okay I'm starting to lose my mind. All I want to do is random a number between 0 and 410, and according to this page, my code should do that. And since I want a random number and not a pseudo-random number, I'm using srand() as well, in a way that…
Jonatan Stenbacka
  • 1,824
  • 2
  • 23
  • 48
2
votes
1 answer

Random Numbers and programming logic

I'm having trouble with the assignment below. "Write a program that generates a random integer between 1-100 and then asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display “Too…
republikunt
  • 41
  • 1
  • 4
2
votes
2 answers

C, Cygwin, and compiling drand and srand

I have a C code which I am trying to compile in Cygwin and which contains both the drand() and srand() functions. I had Windows Vista with Cygwin installed and the code seemed to comile fine, but my computer broke and I had to get a new one. The…
Kaytiana
  • 21
  • 2
2
votes
4 answers

srand(0) and srand(1) give the same results?

srand(0) and srand(1) give the same results. srand(2), srand(3), etc. give different results. Any reason why seed = 0 and seed = 1 yield the same random sequence? Can't find an explanation in the man page. Only that if a seed is not provided, seed =…
João M. S. Silva
  • 1,078
  • 2
  • 11
  • 24
2
votes
3 answers

How to make random value function return different value each time

I've been trying to write a small C function for generating random values. The problem is that it returns same value each time the function is called in for loop. I understand the problem is that srand is seeded with NULL. What I want to know is how…
Sergiy Kolodyazhnyy
  • 938
  • 1
  • 13
  • 41
2
votes
2 answers

What happens behind the scenes if the parameter of srand function is an negative int?

The srand man says that srand has as parameter an unsignd int, but when using without casting the compile does not complain. Have any chance of going wrong if you do not use cast with (unsigned int) ? Or not makes difference, because the compiler…
ViniciusArruda
  • 970
  • 12
  • 27
2
votes
3 answers

Generating Random numbers and printing "Done!" if they fall within a range in C

Sorry if this question has been answered elsewhere, I did search but couldn't find what I was looking for. Anyway, I am stuck on a university homework problem, the problem asks for me to create a script that randomly generates numbers between 0-99…
2
votes
5 answers

C++ rand() each number only once

I need something like rand() but I want each number only once. In example I have 10 ".txt" files that are named as numbers (1-10). I want to read what is inside there and cout it, but randomly. I don't want 1.txt -> 2.txt -> 3.txt -> ... but…
user3566608
  • 353
  • 3
  • 15
2
votes
3 answers

Rand() returns same or very similar output values

C++ question here, using Code::Blocks. I'm trying to run this code to test the pseudo-random function #include #include #include using namespace std; int main() { int count = 0; while (count < 10){ …
Jim_Maru
  • 23
  • 2