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

Recommended Answers

All 11 Replies

Did you seed the random number generator with srand() ?

>>i = rand() % m_QNo;

You want to use 300 there, not m_QNo.

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...

My problem is not random no generation.

Yes it is. Try it out and you will see why the generator is wrong.

@ 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.

commented: Yes, your right. +28

@ 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.

commented: really helpful suggestions +1

@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.

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.