使用VS 2005从访问记录中随机选择的问题 [英] Problem in random selection from access records using VS 2005

查看:69
本文介绍了使用VS 2005从访问记录中随机选择的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨;
我有一个基于Q/A的游戏,它会随机选择存储在访问文件表中的问题.我定义了一个向量和一个stdstream,不带任何问题作为输入.记录数为300,我希望程序仅选择30个问题,但应从300个问题中随机选择,但是我的问题是,当用户选择不带问题的数为30时,只选择前30条记录.的问题是100或300,那么它会考虑100或300条记录.我该如何处理.以下是使用的功能.

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


从m_QNo中随机选择的Getnext问题的功能.
int CGME :: GetNextQuestion()


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

推荐答案

1)rand()是一个质量很低的随机数生成器.请改用Mersenne Twister.
2)实际上,您可以显式确定组合的可能性,并生成一个统一的制服,您可以将其映射到所需的组合.例如,下面的代码列出了组合.如果要计算Combin(N,M),则有几种选择.除非您不小心或处理非常低的#号,否则使用阶乘定义会溢出.斯特林的近似值以及其他一些技巧也很有用.如果您遇到任何需要处理的#溢出问题,我可以写更多有关如何处理它的信息.几年前,我写了一个精算科学/计算统计库.

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.

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);
}


这篇关于使用VS 2005从访问记录中随机选择的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆