i have an array of 16 things, now 8 of them have to be chosen at random so basically what i want to do is

random(CHR/2)

like choose a random number from CHR/2 (as there are 8 not 16 now)

what code would i use because that doestn work

what is "CHR/2" ? If you need 8 random numbers between the values of 0 and 16, then put them in an array.

start loop
    generate random number between 0 and 16
    first time the number has been generated ?
    no, then go back and generate another number
    yes, add to an array of generated numbers
    if 8 numbers have been generated than exit loop
end of loop

to generate a number between 0 and 15 int x = rand() % 16

what is "CHR/2" ? If you need 8 random numbers between the values of 0 and 16, then put them in an array.

start loop
    generate random number between 0 and 16
    first time the number has been generated ?
    no, then go back and generate another number
    yes, add to an array of generated numbers
    if 8 numbers have been generated than exit loop
end of loop

to generate a number between 0 and 15 int x = rand() % 16

rand() is in cstdlib and to make it more random you should seed it first with srand()

to seed the random number with srand the *best* method is as follow.

#include <ctime>
...

srand(time(NULL));

Note you only need to seed rand once in your program.

Chris

This article has been dead for over six months. Start a new discussion instead.