When you're posting a topic, there's an option to not include it in the fresh topics listing. On any remotely technical topic, you really should use that option.
Try running just this line, several times over:
| Code: |
| cout << rand() << endl; |
Note the output.
Now copy / paste that line and rerun your program a few more times.
Notice that on every run the numbers and sequence are the same?
Random generators have what's called a seed. When you call rand(), this seed is used to determine what number it gives you.
And given the same seed, it will predictably output the same "random" number(s).
Meaning that rand() seems to have a rather unchanging seed that is uses in the event that you don't supply one.
So part of the solution to this problem is to set your own seed, using the srand(unsigned int) function.
.. but what are you going to set the seed to? It certainly won't do you any good to set the seed to a constant (an unchanging number).. so what unsinged intenger do you intend to supply it with?
This is where time(NULL) often seems to make an appearance. It gives you an unsigned integer based on your system's time.. which is hardly constant.
Note that you'll need to #include <time.h>
So, inserting a :
.. at the beginning of your code should solve your problem of repeatedly getting the exact same numbers / sequence.
Compiler warning? Change that to: srand((unsigned) time(NULL));
And finally: do you want integers from 1-5, or reals? If you're wanting integers, do you understand how the modulo (%) operator works?
Any positive number mod (%) another positive number will result in a number between 0 and one less than that second number. As in, any positive number % 5 will result in a number between 0 and 4.
Modding establishes a range. If you want a range of 10 potential numbers, you mod by 10. If you want a range of 5 numbers... yeah. And if you want that range to start at a certain point? You add that number.
| Code: |
| int num= (rand() % 5) + 1; |
If you're unsure about this, run that through a calc / on paper a few times for any number you like.. preferably within the range that rand() can actually give you.
If you're wanting real numbers, look at that formula you were given off yahoo.
What are the lowest / highest values rand() can give you?
0-32767, with RAND_MAX being the latter.
Let's find the upper and lower bounderies of your range.
What is 0 divided by RAND_MAX? 0
Times 5? 0.
Plus 1? 1.
Alright, the lower end of your range is where you wanted it.
But what is 32767 divided by 32767? 1.
Times 5? 5.
Plus 1? 6.
Not quite what you had in mind as an upper boundary.
This is corrected easily enough by changing your multiplier from a 5 to a 4, but do you understand why?
Consider the difference between this approach and that of modding. That is, between multiplying and taking a remainder.
Taking the remainder ensures a number between 0 and 1 less than the number you're modding (dividing) by. Count the potential numbers. A number % 5? Range is 0-4. That's zero, one, two, three, four. Five potential numbers.. and your desired range.
Consider the difference between this and multiplying a number by 5; namely that 5 is actually the sixth number.
That's zero, one, two, three, four, five.. for a total of six potential numbers.
And that should explain why your range extends one number moreso than you were wanting it to.
Hoping this helps. Let me know if there are any points you don't understand.
-------
Unbelievably awesomesauce to the absurd degree that I wet myself.
Who else has created a LW/magnets
so explicit the mods deleted a screencap of it? =P