Random value w/o repetition..pls help me..

I am a newbie in C++ programming. I have a project in school using c++ language.
I try to make a program that is a set of random questions that will show the user one by one without repetition of the same questions. i already made the random questions but when i try to make the no repetition code, the output show is not as expected. I already did research from the web, but it seems i cant make it work as expected.

This important of my program where the random number and no repetition code where is..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
int main ()
{
	int arrValue[holderNumQuestion];
	srand( time(NULL)); // This will ensure a really randomized number by help of time.

	
	cout<<"Choose how many questions would you like to answer: ";
	cout<<"\nA. 5\nB. 10\nC. 15"<<endl;
    cin>>numQuestion;
    if(numQuestion == 'a' || numQuestion == 'A')  {
                    holderNumQuestion = 5;
                    }
    else if (numQuestion == 'b' || numQuestion == 'B')       {
         numQuestion = 10;
         holderNumQuestion = 10;
         }
    else {
         numQuestion = 15;
         holderNumQuestion = 15;
         }
    
    for (int countQuestion=1; countQuestion <= numQuestion; countQuestion++)   {
        
        
        bool check;
        int randomHolder;
        
        do{
    	randomHolder = rand()%20;	//to generate a random value or number
    	
		    	
    	check = true;		//check if the value is already used		
    	for (int checkLoop = 1; checkLoop <= countQuestion; checkLoop++ )
    		 if(randomHolder == arrValue[checkLoop]) 	//if number is already used
    		 	{
    		 		check = false;
    		 		break;
    		 	}
    	} while (!check);		//loop to find a new number?
		arrValue[countQuestion]= randomHolder;	//if it is unique, assign it to array variable
		randomHolder = arrValue[countQuestion]; //swap it to this variable
	if (randomHolder == 1)    {
             cout << countQuestion<<". ";
			 cout << "C++ was invented by?"<<endl;
             cout << "a. Dennis Ritchie"<<endl<<"b. Bjarne Struostrup"<<endl<<"c. Bjarne Stroustrup"<<endl<<"Answer: ";
             cin>> inputAnswer;
             if(inputAnswer == 'c' || inputAnswer == 'C')     {
                            cout<<"Your answer is correct!"<<endl<<endl;
                            countScore++;
                            }
             else           {
                            cout<<"You answer is incorrect!"<<endl<<endl;
                            }
             }



The problem is on line 42. When is randomHolder == 1?
So put that part in another for loop and use arrValue[countQuestion] for the if

By the way: An index of an array starts with 0 not 1. Hence your loop should look like this:

for (int countQuestion=0; countQuestion < numQuestion; countQuestion++) // Note: 0 and <
There is a different approach to generating a random sequence.
This does use it: http://www.cplusplus.com/reference/algorithm/shuffle/

Can you spot the difference?
thank you guys for the help and response.. i will try your suggestion and study my code very well.. i will post to my thread again if i did it o not.. thank you very much @coder777 and @keskiverto.
As an extension to keskiverto's response: the example in the documentation gives you a number randomly. That's not exactly the same as a random number -- the numbers that can be returned don't necessarily span the entire range of candidate responses.

For example, an int can hold values in (at minimum) the range [–2147483648, 2147483647], but the given list will only return you values in the range [1,5].

That is to say, there are two kinds of limits on lists of random numbers:
- the range of values they may span
- the number of non-repeating values available

The second is limited by the first, obviously. For example, you cannot have a list of ten non-repeating values choosing only from the numbers 1 through 5.


Now, for a general-purpose solution this indicates that you will have to have additional memory to store your list of random numbers.

The list requires some additional overhead maintenance: it must be able to tell you whether a potential member is already present or not. Fortunately, the STL provides this for us in the std::set.

Finally, for a runtime issue: the overhead takes time.

If pulling numbers is required to go faster than creating the set of numbers, then make it a set of numbers to choose from. When the next random number is needed, remove one from the set.

If the time it takes to create the set must not produce any noticeable delay (and you can't offload it in some other way), then make it a set of numbers already chosen. When the next random number is needed, generate one until a unique one can be added to the set.

Notice how populating the set of random numbers works the same for both distinctions. The difference is when a random number is added to the set: all at once (at some point in your program), or only when a new number is needed.


One final caveat: The closer N is to the range of potential random numbers, the longer it will take to populate the list. You can fix this by populating the list with all possible values then randomly removing values until the list is the proper length.

Don't forget to be careful to design your algorithm for what you need it to do!


Hope this helps.
Topic archived. No new replies allowed.