random number function

So Im working on a project and it calls for a random number of items in a linked list, accessing these functions according to a timeline, then removing or adding objects in the linked list based on certain conditions. Im having a bit of a brain "fart" when it comes to using the random number algorithm

rand() % 100 keeps giving me the number 41 and I can't seem to find out why. Any ideas?
You need to seed it with srand() and time()

The syntax is:
1
2
3
4
5
6
7
8
9
#include <cstdlib>
#include <ctime>

int main()
{
   std::srand(unsigned( std::time(0) ));

// rest of code.
}


You only should seed once at the beginning of main.
ah much better, thank you. Now for my next question, the assignment calls for arguments based on the random number it picked, how do I call upon this number later in the code for while/if/for statements? Also, is it possible to do multiple random numbers?
Assign the value from rand() to a integer variable.
int myInt = rand();
The variable will hold that random number throughout the code (as long as you don't reassign).

For multiple values, use a loop and an array of integers (or the same one but call rand() at a later time.)
1
2
3
4
5
6
7
8
9
10
int array[5];
for (int i = 0; i < 5; ++i)
   array[i] = rand();

//OR

int val = rand();
// MORE CODE HERE
// Some time later
val = rand();
excellent, I think I got it from here. thank you for your help!
Topic archived. No new replies allowed.