The need to generate random numbers without a recurrence . It may be useful for various things such as randomly ordered list items, forms random groups, shuffling cards, etc.. So algorithm would be something like:
- Create an array and leave it empty.
- Get a random number.
- Check if that number exists in the array. If yes, return to step 2. If not, save the number in the matrix.
So the program is this:
First of all, <cstdlib> is the correct way to include the library in c + + <stdlib.h>, same with <ctime>.
You see, we use a bool function ( which returns true or false ) to check if the random number already exists in our matrix. If any of the numbers is equal to our random number n , the function returns true , so that the cycle do-while since we call the function will be repeated again until the function returnsfalse .
When it gets out of do-while loop, we keep our n in place of the corresponding matrix, as shown on the screen and continue to the next position in the array.
0 comments:
Post a Comment