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

Creating a random story generator using pointers and arrays to deal with strings

I am trying to figure out how I should structure my next lab for programming class. Here's what I have so far. Questions go line by line: int main() { char name, color, person; //color, pet and car are the only arrays. The rest will be…
-2
votes
3 answers

c++ using random numbers to decide the next encounter

EDITED for full code I am trying to make a text based rpg game because im really bored and want to put my c++ "skills" at test xd. But i am having a problem with the functions srand and rand, the function to generate random numbers. What i want…
cppnewb
  • 1
  • 3
-3
votes
1 answer

Unable to compile - scope issues

I have copied program on Simulated Annealing from a book (first result link here) and am facing the below issues on compilation, for below line in main(). srand48(tp.tv_usec); Error on compiling with Dev-C++: [Error] 'srand48' was not declared in…
jiten
  • 193
  • 2
  • 10
-3
votes
1 answer

randomly increment through an array, c++

I am struggling to understand the concept of randomly reading numbers from an array of integers using 'rand()'. I have created a random number generator between 1-3 and want to output an index of an array, then for the generator to randomly output…
C.Mann
  • 11
  • 8
-3
votes
1 answer

Basic minesweeper. Bomb counter function not working

I've been working on some code for a basic minesweeper program and at this point I'm complacently lost. The numbers around the bombs wont generate properly and there doesn't even seem to be a pattern to whats going wrong. I need help with finding a…
-3
votes
1 answer

C++ Randomize Different Variables Using a Constructor

Robot::Robot() fitness = 5; totalBattery = 5; int randNum; int i = 0; bool valid = true; srand(time(NULL)); //get robot's position on board and check to see if it is not starting on the wall while (valid) { position = (rand()+i++) %…
Rwal27
  • 1
-3
votes
2 answers

c++ srand() not working

I didn't understand what everyone said here: srand(time(NULL)) doesn't change seed value quick enough and I am supposed to use the srand() to generate a different random number every time but I keep getting the same number, any…
Amal
  • 37
  • 1
  • 5
-3
votes
4 answers

srand doesn't help me creating random numbers

I'm trying to shuffle the cards in a deck. I create two numbers randomly and assign their values to eachother 100 times. But it doesn't create randomly. Weirdest part is this, I put a breakpoint to line "counter++;" to observe if the program really…
K.Yazoglu
  • 207
  • 3
  • 13
-3
votes
1 answer

How do I store a random number in C++

So far, I have made a program that creates a random number using srand and rand. #include #include #include using namespace std; int main() { srand(time(0)); for(int x = 1; x<25;x++) { cout << 1+…
user5356957
-3
votes
2 answers

Why isn't srand seed changing results?

I hope the answer isn't too blatantly obvious, but I've looked around and can't seem to figure out why srand isn't changing the values in the bull_pen vector. Every time it runs I get the same 4 integers. I've read all about using the computer…
Brendan
  • 11
  • 3
-3
votes
2 answers

srand(time(0)) not making random numbers?

Here is the code, but the outputs aren't coming out random? Maybe cause when the program runs it has the same time as all the loops? #include using namespace std; #include #include #include #include…
TheStache
  • 29
  • 1
  • 4
-3
votes
1 answer

A Text-Based Zombie Game I made. (HELP!)

#include #include #include #include using namespace std; void divider(); int main() { int cstats, choice; int rhp, hp, i, init, atk, def, matk, mdef, dmg, mdmg, agi, magi; divider(); cout <<…
Supremo
  • 29
  • 3
-3
votes
3 answers

srand(time(NULL)) "Function 'srand' could not be resolved."

I have been trying to debug this problem for a while and quite honestly, I just can't see what I'm doing wrong. Why is there a syntax error? #include ; #include ; #include ; #include ; using namespace…
Haque1
  • 575
  • 1
  • 11
  • 21
-4
votes
4 answers

How can I generate a random number between 5 and 25 in c++

Possible Duplicate: Generate Random numbers uniformly over entire range C++ random float How can I generate a random number between 5 and 25 in c++ ? #include #include #include using namespace std; void main() { …
faressoft
  • 19,053
  • 44
  • 104
  • 146
-4
votes
1 answer

How can i generate random numbers with rand()?

I need help with this code I have written. The problem is that I want it to generate random numbers from 1000 to 9999. But if you run it, it returns numbers higher than 9999. I have read some forums about the correct way to use rand() but am still…
1 2 3
26
27