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

My problem is not random no generation rather I need to select 30 random records from query table out of 300 records. At the moment, if my player selects 30 questions options, he gets only the first 30 questions from collection of 300 records, I want this selection to be randomly all over 300 records.

My problem is not random no generation rather I need to select 30 random records from query table out of 300 records. At the moment, if my player selects 30 questions options, he gets only the first 30 questions from collection of 300 records, I want this selection to be randomly all over 300 records.

You are incorrect, your random number generation IS your problem. We understand what you are trying to accomplish. Look more carefully at what AD told you and think a little more about what your RNG does and how the value is used. You are only choosing from a limited range because your RNG isn't implemented properly.

Don't base your random numbers (your selection range) on the user input. Base the random numbers on your record count at all times. Then, use the user input to control the count of loop iterations.

//get user input
cin >> noOfQuestions;

//build your question array
for (int qNo = 0; qNo < noOfQuestions; qNo++) {
  //generate random number
  //check if question is already used
  //perform dBase query
  //store question
}
// etc...

Edited 6 Years Ago by Fbody: n/a

@ AD
when I change m_QNo with 300 ..
it gives runtime error. I am higlighting the error. Can I use rand()%300 alongwith srand(time(NULL)) for random generation. But I think resizeing the Query table is the rason for problem.

else
{_SCL_SECURE_VALIDATE_RANGE((this->_My_actual_offset() + _Off) <= ((_MycontTy *)this->_Mycont)->_Mysize);

Call srand(time(NULL)) only once during the lifetime of your program. I normally put it near the beginning of main().

You don't replace m_QNo with 300 everywhere in your program. Only in that one function with rand().

I have no idea what that _SCL_SECURE_VALIDATE_RANG macro does.

You will need to change this line as well m_Qtable.resize(m_QNo); as well and in fact you will need to change all instances of m_QNo in your code, both posted and unposted where you have accidentally used the question number rather than the number of available questions as your limit for accessing the question table.

Edited 6 Years Ago by Banfa: n/a

Comments
Yes, your right.

@ Banfa
In my code, I have define std vector m_QTable and using

p.DoModal();
CPerson p;

for input (p.question)
to the

stdstream sa

where p.question is the no of questions selected by user.
But I don't get how I define and use the output from sa stream for resizeing Query Table. stdstream (in/out) could only be (Cstring, int) ?

You don't use the the output from the stringstream sa to resize your query table.

That is what everyone has been saying. Your query table has a fixed size, that is the number of defined questions, 300 from your first post.

You have 2 options either you select question randomly as you ask them. In this case you need a table with an entry for every question (300) which you use to initially mark all questions as unused and as you ask a question you mark it as used so that if it gets selected again then you can make a new selection rather than asking the same question.

Another option is before asking any questions build a list of the question you will ask that references the complete table of questions.

Ignoring possible problems if the number of questions being asked is a large proportion of the number of available questions those 2 different method both require an additional table that is not the query table.

the query table remains a fixed size it is the ancilary tables whose size may alter depending on the method used to record questions asked/to ask.

Comments
really helpful suggestions

@Banfa

Thanks for your suggestions. They were really helpful. I solved the problem by using one function for random generation and other for the table index. It worked this way.
I have one more thing to ask, how can I manage to lock or unlock this access db query table. Like during the game play, it should remain lock till it is accesed, so that player can't knwo what is inside db.

This question has already been answered. Start a new discussion instead.