Just so that there's no possibility for confusion.
Here's how rand() works:
rand() will return a pseudo-random number between zero and
RAND_MAX.
RAND_MAX is a macro that's defined in the
cstdlib header. What this value is, is library dependent, but it's guaranteed to to be at least 32767 on any standard library implementation.
On my system,
RAND_MAX is defined in stdlib.h, which is included by cstdlib, but that's a technicality.
On my system,
RAND_MAX is defined as 0x7fff, which is equivalent to 32767.
Therefore, if you write:
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <iostream>
#include <cstdlib>
int main() {
int value = rand();
std::cout << value << std::endl;
std::cin.get();
return 0;
}
|
It should print some value between zero and whatever your definition of RAND_MAX is. For me, it prints out 41.
However, it will always print the same number, regardless of how many times we run the program. This is where the "pseudo" part comes in. Before you use
rand(), you have to "seed" it with
srand().
rand() doesn't really return a random number. It returns the result of a seemingly random calculation. One of the variables involved in this calculation is the "seed", which means that if you want "random" numbers, you need to provide a seed that's different every time you run the program.
This can be achieved by providing the system time as a seed, since the system time will be different practically every time the program is executed, which is good enough for most beginner purposes.
Typically, one seeds rand() at the beginning of the program, and you must only do so once.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
#include <ctime>
int main() {
srand(time(0));
int value = rand();
std::cout << value << std::endl;
std::cin.get();
return 0;
}
|
This should print a different value every time the program is executed.
The values that rand() returns to us only become really useful, when it allows us to generate numbers within a certain range.
We can make use of the modulus operator (%). The modulus operator finds the remainder of a division of one number by another.
Given two integers 'a' (the dividend), and 'n' (the divisor), the expression "5 mod 2" would evaluate to 1, because 5 divided by 2 leaves a quotient of 2 and a remainder of 1.
This allows us to effectively "limit" the numbers being generated with rand(). To be more specific, our random number will be the result of the division of the value returned by rand(), by the largest, maximum value from our desired range.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
#include <ctime>
int main() {
srand(time(0));
int value = rand() % 10;
std::cout << value << std::endl;
std::cin.get();
return 0;
}
|
This will print a "random" number between zero and ten (0-9).
This is fine as long as your desired number range is above zero.
Anyways, I hope you realize why your snippet is wrong. As the others have already stated, what you're doing is assigning new "random" values to each element of the array. You aren't actually "randomly" shuffling the order of the elements if that's what you were trying to achieve.