Customizable size and number of random number functions

Source: Internet
Author: User
Tags header rand

Recently in doing a test software, need each topic different and the number of questions can be customized, in C + + did not find the applicable function, on the easy to do a more general C + + random function expansion, please programming reference.

void randEx(int MAX,int NUM)
{
  int k=0;
  int j=0;
  time_t t;
  //设置rand函数所用的启始种子值,以期每次产生的随机数序列均不相同。
  srand((unsigned) time(&t));
  for (k=1;k<=NUM;k++)//定制随机数数量。
  {
    RAND[k]=rand()%MAX;//定制随机数在0至最大值之间。
    do 
    {
    for (j=1;j<k;j++) if (RAND[j]==RAND[k]) //一次随机数序列中有相同随机数则再
              //产生一个,直至一次随机数序列中随机数全不相同。
    {
      RAND[k]=rand()%MAX;
      break;
    }
    }while(j<k);
  }
}

The specific usage: first in your project corresponding class header file defines an integral type array rand[], adds this function to the header file, passes the maximum value and the quantity in the corresponding class realization, outputs the random number sequence. See the example project, the main code in the example project is as follows:

Testranddlg.h:header file
//
Class Ctestranddlg:public CDialog
{
Public
Ctestranddlg (cwnd* pparent = NULL); Standard constructor
int rand[20];
void Randex (int max,int NUM);
cwnd* P_staticwnd;
.....
}
TestRandDlg.cpp:implementation file
//
.....
void Ctestranddlg::ontestrand ()
{
UpdateData (TRUE);
Randex (M_nrandmax,m_nrandnum);

CString str;
if (m_nrandnum>=1) {
Str. Format ("%d", rand[1]);
P_staticwnd = GetDlgItem (IDC_STATIC1);
P_staticwnd->setwindowtext (str);
if (m_nrandnum>=2) {
Str. Format ("%d", rand[2]);
P_staticwnd = GetDlgItem (IDC_STATIC2);
P_staticwnd->setwindowtext (str);
if (m_nrandnum>=3) {
Str. Format ("%d", rand[3]);
P_staticwnd = GetDlgItem (IDC_STATIC3);
P_staticwnd->setwindowtext (str);
if (m_nrandnum>=4) {
Str. Format ("%d", rand[4]);
P_staticwnd = GetDlgItem (IDC_STATIC4);
P_staticwnd->setwindowtext (str);
if (m_nrandnum>=5) {
Str. Format ("%d", rand[5]);
P_staticwnd = GetDlgItem (IDC_STATIC5);
P_staticwnd->setwindowtext (str);
if (m_nrandnum>=6) {
Str. Format ("%d", rand[6]);
P_staticwnd = GetDlgItem (IDC_STATIC6);
P_staticwnd->setwindowtext (str);
if (m_nrandnum>=7) {
Str. Format ("%d", rand[7]);
P_staticwnd = GetDlgItem (IDC_STATIC7);
P_staticwnd->setwindowtext (str);
if (m_nrandnum>=8) {
Str. Format ("%d", rand[8]);
P_staticwnd = GetDlgItem (IDC_STATIC8);
P_staticwnd->setwindowtext (str);
if (m_nrandnum>=9) {
Str. Format ("%d", rand[9]);
P_staticwnd = GetDlgItem (IDC_STATIC9);
P_staticwnd->setwindowtext (str);
if (m_nrandnum>=10) {
Str. Format ("%d", rand[10]);
P_staticwnd = GetDlgItem (IDC_STATIC10);
P_staticwnd->setwindowtext (str);
UpdateData (FALSE);
}

This project is debugged and passed in Vc++6.0+windowsxp.

This article supporting source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.