Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi;
I have a Q/A based game which randomly selects questions that are stored in access file table. I have define a vector and a stdstream that takes no of questions as input. The no of records are 300 and I want the program to select only 30 questions but they should be slected randomly from 300, but my problem is it just selects the first 30 records when user chooses no of quetion to be 30. If user chooses no of questions to be 100 or 300 then it takes into account 100 or 300 records. How can I manage this. Below are functions that are used.

std::vector<bool> m_Qtable; // vector defination

std::stringstream sa; //string stream 
	sa << p.question;	//input
	sa >> m_QNo;        //output
	m_Qtable.clear();
	m_Qtable.resize(m_QNo); 
qnum = GetNextQuestion();
m_db.m_ID = qnum; //record ID is taken as question num

Function for Getnext question which is randomly selectd from m_QNo.
int CGME::GetNextQuestion()
{
	int i;
	i = rand() % m_QNo;
	while(m_Qtable[i])
		i =rand() % m_m_QNo;
	m_Qtable[i] = true;
	return i+1;
}


I'll be waiting for your feedback. If above code is not much for understanding the problem , I can provide further information.
Regards
Posted

1 solution

1) rand() is a very low quality random number generator. Use the Mersenne Twister instead.
2) You actually can explicitly determine equally probably the combinations and generate a single uniform, which you can map to the combination you desire. For example the code below lists the combinations. If you want to compute Combin(N, M), you have a few choices. Using factorial definitions overflows unless you are not careful or deal with very low #s. Stirling's approximation is useful as well as several other tricks. If you have overflow issues with whatever #s you need to deal with, I can write more about how to handle it... I've wrote an actuarial science / computational statistics library several years ago.

C#
namespace {
    void print(int *s, int const & k) {
        for (int j=0; j<k; j++)
            printf("%i ", s[j]+1);
        printf("\n");
    }
    int diff_sum(int *s, int const & k) {
        int sum=0;
        for (int j=1; j<k; j++)
            sum+=s[j]-s[j-1];
        return sum;
    }
};
void combination_list(int const & N, int const & k) {
    int selection[k], reset[k-1];
    selection[0]=N-k;
    int i;
    for (i=1; i<k; i++)
        selection[i]=reset[i-1]=N-k+i;
    int count=1;
    print(selection, k);
    int j;
    while (selection[0]+diff_sum(selection, k)>=k) {
        for (j=0; j<k; j++) {
            if ( diff_sum(&selection[j], k-j)==k-1-j) {
                selection[j]-=1;
                for (i=j+1; i<k; i++)
                    selection[i]=reset[i-1];
                count++;
                print(selection, k);
            }
        }
    }
    printf("count = %i\n", count);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900