OK I need some help... so far ive been able to get everything on my own but...im making a program that selects questions from an array numbered 1-40. i put in a random fuctions so the questions are chosen randomly. but i want to have it so that theres no duplication in the questions. like if the number is already chosen... skip and pick the next. any help would be much appreciated.

## All 8 Replies

It seems easy enough to set up another array to store a reference of string pointers to the questions chosen. And each time a random question is selected search the other array for a matching pointer. Make sure you set each of the other array cells to NULL so you don't compare some unknown something. :)

Of course I don't know C++ but that's how I'd do it in C. I'd probably set the array only to 10 pointers and run it like a queue once it's filled up. A repeat after 10 questions wouldn't be bad.

Here's an extremely simplistic/mediocre way of doing it in C++, based on Aelfinn's posted.

Lets say you have array of strings A with 40 cells, that looks sorta like the following?

A = Question 1?
A = Question 2?
A = Question 3?

and, well, you get the idea. Now, basically create a second array, int B. Also create a variable int so_far which will be an accumulator each time a question is asked.

So, lets say a random number 5 is chosen. First, look up A and ask the question. Then, set B equal to 5, and increment so_far by 1. Now, lets say a random number 8 is chosen. Look up A, ask the question, and set B equal to 8, and increment so_far by 2. Each time a question is asked, do a linear search on B through B[so_far - 1] to see if any of the values match up to 8. Make sense?

One easy way to achieve this would be as follows:

- Initialise a counter to the number of strings (40)
- Select a random string in the range 1 to 40 (e.g. string 10)
- Swap the selected string (10) with the last unused string (40)
- Output string 40
- decrement the number of strings (to 39)

The string you used is now outside the range (as string 40)

- Select a random string in the range 1 to 39 (e.g. string 26)
- Swap the selected string (26) with the last unused string (39)
- Output string 39
- decrement the number of string to 38

The used strings are at positions 39 and 40, outside of the range

- repeat as required, handle the condition whereby zero strings are left

It sounds more complicated than it really is, here's a function that achieves it (using a vector of strings):

``````string getQuestion&#40;vector<string>& v, int& numberOfStrings&#41;
&#123;
// handle errors
if&#40;numberOfStrings <= 0&#41; return "error";
if&#40;numberOfStrings > v.size&#40;&#41;&#41; return "error";

// select a random number in the range 0 - numberOfStrings-1
int next = rand&#40;&#41; % numberOfStrings;

// decrement the number of strings
numberOfStrings--;

// swap it with the last unused string
swap&#40;v&#91;next&#93;, v&#91;numberOfStrings&#93;&#41;;

// retrieve the string
return v&#91;numberOfStrings&#93;;
&#125;``````

You populate your vector with strings, then set an integer variable equal to the number of strings, and off you go - make calls to the getQuestion() function.

Here's a demo program:

``````#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <algorithm>

using namespace std;

string getQuestion&#40;vector<string>& v, int& numberOfStrings&#41;
&#123;
if&#40;numberOfStrings <= 0&#41; return "Error";
if&#40;numberOfStrings > v.size&#40;&#41;&#41; return "Error";

int next = rand&#40;&#41; % numberOfStrings;
numberOfStrings--;
swap&#40;v&#91;next&#93;, v&#91;numberOfStrings&#93;&#41;;
return v&#91;numberOfStrings&#93;;
&#125;

int main&#40;&#41;
&#123;
vector<string> q;

q.push_back&#40;"One"&#41;;
q.push_back&#40;"Two"&#41;;
q.push_back&#40;"Three"&#41;;
q.push_back&#40;"Four"&#41;;
q.push_back&#40;"Five"&#41;;
q.push_back&#40;"Six"&#41;;
q.push_back&#40;"Seven"&#41;;
q.push_back&#40;"Eight"&#41;;
q.push_back&#40;"Nine"&#41;;
q.push_back&#40;"Ten"&#41;;

int num=q.size&#40;&#41;;

cout << getQuestion&#40;q,num&#41; << endl;
cout << getQuestion&#40;q,num&#41; << endl;
cout << getQuestion&#40;q,num&#41; << endl;
cout << getQuestion&#40;q,num&#41; << endl;
cout << getQuestion&#40;q,num&#41; << endl;
cout << getQuestion&#40;q,num&#41; << endl;
cout << getQuestion&#40;q,num&#41; << endl;
cout << getQuestion&#40;q,num&#41; << endl;
cout << getQuestion&#40;q,num&#41; << endl;
cout << getQuestion&#40;q,num&#41; << endl;
cout << getQuestion&#40;q,num&#41; << endl;
cout << getQuestion&#40;q,num&#41; << endl;
&#125;``````

I like that...much faster than my idea and with no guaranteed work to prove the string hasn't been used.

Note that you'd want to add a call to srand() at the start of main() in your program. It suited me not to do this during testing so that it always used the same sequence.

ok...
if i were on your place then i will try to do it some thing like that

i will make another array of same size of or of the size greater than that of your array but as it was wastage of memory so make array of size as u need and after that i will place random number in that array and if that number comes by using some search technique i will check whether this number comes before if it was... then i will again generate random number and so on
hope u will understand....:)

commented: Special bonus for bumping a thread nearly 7 YEARS OLD -4
commented: I wonder if I0adidas0I can finish his homework now. -2

2 approaches to consider:

1st assuming that you have a list of questions that are unique
in something like a std::vector<std::string> questions

there are two approaches I would recommend #2

#1
Shuffle the questions so that they are in a random order
You can use a method suggest by the other posters

have an iterator (something to point to the contents)
point to the  string of the shuffled questions
std::string<std::vector>::iterator it(questions.begin());

on get_question you can get current_string
*it (to get the value the iterator is pointing to) in this case a string
++it; //move onto next

need to check (it != questions.end())
as this would mean you have run out of questions

#2
leave the questions fixed in order

have an index of ints

``````int number_of_questions = questions.size();
std::vector<int> index;
for(int i = 0; i < number_of_questions; ++i)
{
index.push_back(i);
}``````

Now you can shuffle the index to give a shuffled index
then when you want a question get the number

``````//when a new asker arrives
index = shuffle(index);
std::vector<int> shuffled_index = index;

int ind = 0;``````
``````//now each time when you get a question
//check ind ok
if(ind >=0 && ind < shuffled_index.size())
{
int cur_ind = shuffled_index[ind];
//use current ind to get the question
if(cur_ind >= 0 && cur_ind < questions.size())
{
std::string question = questions[cur_ind];
}
//get the next random id
++ind
}
else
{
}``````

you only need to randomise when you shuffle