VC Learning Data Collection (21): Customizable Size and Quantity Random Digital Function

xiaoxiao2021-03-06  41

Customizable size and number of the function of a random number: 93 106 troops Yuan Haihong

Download This article is that the source code is in the case of a test software, and the number of questions can be customized each time. If the number of the number of theme is not found, it will make a more common C random function extension, please program the reference .

Void Randex (int max, int Num)

{

INT K = 0;

INT j = 0;

Time_t t;

// Set the starting seed value used by the RAND function, in order to do not have the same random number sequence each time.

SRAND (& T);

FOR (k = 1; k <= NUM; k ) // custom random number number.

{

Rand [k] = rand ()% max; // custom random number between 0 to max.

DO

{

For (j = 1; j

// Generate one until the random number is not the same in a random number.

{

Rand [k] = rand ()% max;

Break;

}

WHILE (j

}

}

Specific usage: First define an integer array RAND [] in your project in your project, add this function to the header file, pass the maximum and quantity in the corresponding class implementation, and output the random number sequence. For details, please refer to the example project, the main code in the sample project is as follows:

// Testranddlg.h: Header File

//

Class Ctestranddlg: Public CDIALOG

{

PUBLIC:

Ctestranddlg (CWND * PParent = NULL); // Standard Constructionor

Int Rand [20];

Void Randex (int max, int num);

CWND * P_STATICWND;

.....

}

// Testranddlg.cpp: importation 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 debugged in VC 6.0 WindowsXP.

转载请注明原文地址:https://www.9cbs.com/read-75495.html

New Post(0)