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

C++ and rand/srand behavior not performing as expected

I am using rand and srand from cstdlib and g++ as a compiler. I was playing around trying to generate some pseudo random numbers and I was getting some unexpected biased results. I was curious so I wrote a simple function. The expected behavior…
-1
votes
3 answers

How can I generate different random numbers for each player?

Both players get the same random number! ّI want each player to get a different number since they are throwing dice. here is the code: #include #include #include int roll_a_dice(void); int main(int argc, const char…
RedDoumham
  • 51
  • 7
-1
votes
1 answer

Produce a random seed to give to srand() - Not time(NULL)

I am currently writing a level generation program for the game Sokoban. I use the rand() function quite a lot to generate each level, and I thought that a nice feature would be for the user to be able to control the seed used for the generation,…
Conor Watson
  • 567
  • 1
  • 7
  • 28
-1
votes
1 answer

Using rand() and srand(time(NULL)) right after doesn't generate a new random number

for (x = 1; x < 10; x++) { (armoPtr + x) ->def = rand() % b + a; if ((armoPtr + x) -> def > b) (armoPtr + x) -> def = b; srand(time(NULL)); (armoPtr + x) -> hp = rand() % 5 + 1; srand(time(NULL)); (armoPtr + x) ->…
-1
votes
1 answer

Modifying rand() ranges c++

I'm trying to make a guessing game program where the user thinks of a number between 1 and 100, and then the computer randomly generates(guesses) a number between 1 and 100. Then the user tells the computer if their number is higher(H), lower(L), or…
-1
votes
1 answer

Error reading variable (cannot access memory at address) encountered when srand() called

I'm experimenting with concurrent programming in C using the Codeblocks IDE with gcc. When I run my program, I receive no output. Interestingly, though, when I put a breakpoint at a certain point in the program, the program will execute all…
Johnny
  • 105
  • 1
  • 13
-1
votes
5 answers

How to generate a random number within a range once in C?

New to programming in C and I'm trying to generate a random number once. But when the program complies it generates 30 random numbers in the range that the users specifies. Would using an Array be easier? Any help would be good. edit the program…
nm10563
  • 43
  • 1
  • 9
-1
votes
2 answers

How can I get rand to give me proper number using accuracy?

I had a lab where we had to write a program to simulate the duel, for Aaron who a has probability of 1/3, Bob 1/2, Charlie never misses. The program should use random numbers and the probabilities given in the problem to determine whether a shooter…
Csci319
  • 125
  • 8
-1
votes
1 answer

using srand to insert random numbers

I am having trouble with my syntax for calling srand to insert random numbers for an addition problem?? tried looking up a similar program to view syntax with no luck. #include #include"simpleio.h" int main() { // int…
cyerocks
  • 1
  • 2
-1
votes
1 answer

C++ Guessing a number within a range using srand(time(0))

Alright so here's my question... I pretty much finished my little game I made, but I want to test to see if it works with a "debug cheat" to see if the program is actually showing you the right number. I have srand(time(0)) set so that when you play…
Jordan
  • 69
  • 1
  • 11
-1
votes
1 answer

ANSI C / rand() % 7 first value is always 3

So trying to figure out what is special about rand() mod k7 in C. In another post someone said that C rand() uses http://en.wikipedia.org/wiki/Linear_congruential_generator but I don't see what makes (mod k7) k-> scalar special for the algorithm…
Thomas
  • 201
  • 1
  • 3
  • 12
-1
votes
2 answers

Loop within a loop & random generation in c

I've been scratching my head for too long on this. I saw many topics on how to hadle random generation within a C loop, however I haven't seen anything when two loops are involved. Here's my code below : typedef struct { char qh_uid[6]; long…
kv000
  • 131
  • 5
-1
votes
1 answer

srand is too slow in C++ it return same number

I'm just trying to have simply RndInt(limit) function that will return random numbers with a limit as limit. cout << "Enter higher limit of random range:" ; cin >> limit; while(limit != 0) { // RndInt(limit); cout << "Random number…
MarkoShiva
  • 83
  • 1
  • 2
  • 10
-2
votes
1 answer

I'm new to functions and I can't figure out how to correct the sum integer

This is the code I currently have: #include #include #include #include using namespace std; int random (int sum); bool isValid(int userNum, int sum); int main() { int whoTurn, playAgain = 1, userNum, sum; …
-2
votes
1 answer

Rand() producing the same results every run

I am entireley new to coding, and am coding a program to simulate catching pokemon. I have a bunch of random numbers being generated during it, but the one that randomizes which pokemon (In int main()) is not randomizing, its only producing "1".…