C language and MFC random array generation

Source: Internet
Author: User
Tags random seed
C language/C ++ generates random numbers. here we use the rand () function, srand () function, and C language/C ++ without the built-in random (intnumber) function. Trigger the event with a button, convert it to 10 random numbers, load the array, convert the array elements to the CString type, and splice them, and then display them on the EditControl control.

C language/C ++ generates random numbers. here we use the rand () function, srand () function, and C language/C ++ without the built-in random (int number) function.

Let's take a look at the generation of random arrays in MFC. the C language also uses the same method:

void CNM_MFCDlg::OnBnClickedOk(){CEdit* pBoxOne;pBoxOne = (CEdit*) GetDlgItem(IDC_EDIT1);CString showStr;char tmp[10] = "";int rand_num[10];CString str[10];CString result;srand((unsigned)time(NULL));for(int x=0; x < 10; x++){rand_num[x] =  rand()%100;str[x] = itoa(rand_num[x],tmp,10);result = result + str[x] + _T(" ");}pBoxOne-> SetWindowText( result );result.ReleaseBuffer();}

Program running result:

Trigger the event with a button, convert it to 10 random numbers, load the array, convert the array elements to the CString type, and splice them, and then display them on the Edit Control. The method for generating random arrays is the same as that in C.

If you only need to generate a random number without setting a range, you only need to use rand (): rand () returns a random value ranging from 0 to RAND_MAX. RAND_MAX is defined in stdlib. h and its value is 2147483647.

#include 
 
  #include 
  
   void main(){for(int i=0;i < 10;i+)printf("%d\n",rand());}
  
 

If you want to generate a random number in a certain range, you can define a random (int number) function in the macro definition, and then directly call the random () function in main.

For example, a random generation of 10 0 ~ Number of 100:

#include
 
  #include
  
   #define random(x) (rand()%x)void main(){for(int x=0;x<10;x++)printf("%d\n",random(100));}
  
 

However, the random numbers generated in the preceding two examples can only be one-time. if the output result is still the same as that of the first running. This is related to the srand () function. Srand () is used to set the random number seed when rand () generates a random number. Before calling the rand () function to generate a random number, you must use srand () to set the random number seed. If no random number seed is set () during the call, the random seed is set to 1. In the above two examples, because no random number seed is set, each random number seed is automatically set to the same value of 1, which leads to the same random value produced by rand.

Srand () function definition: void srand (unsigned int seed); generally, the return value of geypid () or time (0) can be used as seed. if you use time (0, header file # include

#include
 
  #include
  
   #include
   
    #define random(x) (rand()%x)void main(){srand((int)time(0));for(int x=0;x<10;x++)   printf("%d\n",random(100));}
   
  
 

In fact, there are two lines of code:

Srand (unsigned) time (NULL); // time (NULL) returns from 1/1/1970 in seconds // srand (time (NULL )); int temp = rand ()/nMax; // [0, nMax) the integer range you want

This article is available at http://www.nowamagic.net/librarys/veda/detail/375.

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.