Hey guys,

I m making a program that selects questions from an array numbered 1-40. I put in a random functions so the questions are chosen randomly.

But i want to have it so that theirs no duplication in the questions like if the number is already chosen skip and pick the next.

Any help would be much appreciated.

The usual approach here is to "shuffle" the numbers and then pick them off one by one. (Code untested.)

// Load array
for (int i = 0; i < 40; ++i)
    n[i] = i + 1;
// Shuffle
for (int i = 39; i > 0; --i) {
    // Pick rnd position and switch that element with i
    int r = rand() % (i + 1);
    int t = n[r]; n[r] = n[i]; n[i] = t;
}
// Use them in array order
for (int i = 0; i < 40; ++i)
    doWhatever (n[i]);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.