A fast and prefabricated random array generation method

Source: Internet
Author: User
Tags arrays generator rand

This paper introduces a simple, fast and practical method of random array generation, which is passed through debugging. Attachments for All program code please review.

In the design of engineering software and safety system, it is often necessary to use random array to build model and generate password. The computer, however, does not produce an absolutely random random number, and the computer can only produce "pseudo random numbers". In fact, the absolute random random number is only an ideal random number, even if the computer development, it will not produce a series of absolute random random number. A computer can only generate a relative random number, or pseudo random number.

Pseudo-random number is not a false random number, but refers to a regular number, in fact, the computer is calculated by a certain algorithm. The common approach is to use a given number as a seed, such as taking the time of change as a seed, calling Srand (unsigned) (NULL) to perform rand () to obtain a random number. It is clear that the random numbers of the same or similar seeds will be exactly the same or close to each other.

Therefore, to produce random numbers of real meaning, the seeds must first be random. Random seeds can be generated by an external hardware random generator, and it is said that the latest Intel processor uses a method of reading thermal noise on the CPU to obtain random numbers. When a random generator of hardware is not available, the usual approach is to add a time delay between the two call Srand (unsigned) times (NULL). But when it comes to generating a large random array, the delay is long and intolerable. In fact, due to the regularity of the delay, the random number produced is not so random.

An easy way to produce random arrays is to design complex algorithms to reduce the regularity of array results. This approach requires a high skill and is not suitable for general program calls.

In addition, in many cases, we not only require the generation of random arrays but also the statistical regularity of the resulting arrays. If must obey the normal distribution, evenly distributes and so on. Simple calls to the Srand and RAND functions do not meet the needs.

MATLAB is a powerful engineering mathematics software, using its random array generation module, we can easily get a large array of different distribution forms, the principle of its random number generation is based on complex algorithm. Therefore, we naturally think of using the random tables produced by MATLAB as random number pool, from which we need random array.

This random array generation method consists of three parts: one is the random number of TXT file pool, where I use MATLAB to generate 1000 random number of normal distribution, 10 a row, every two digits between 3 spaces, the beginning of 3 spaces as a text document, the first number is 0, Then the column ordinal number is sorted to 999, and the second and third are the. h files and. cpp Files of the function class that take the random number from the random number pool.

In a function class that takes a random function, define the CStdioFile file1 and open the TXT document as a random number pool. Start with time as a seed, generate a random number of 0-999, and read the number of random numbers from the beginning of the number in the pool until you read enough random arrays. If the ordinal number of the pool is exceeded, skip to the start of the pool and continue reading.

Call this function class, you need to enter the int m_collect_times,double *a_random,int m_txt_line,int m_txt_row,int m_txt_spacing, respectively, to represent the number of points taken, the location of the random array stored , number of random pool rows, number of columns, and number of spaces between the two numbers in the pool. and change "C:\\yg\\debug\\ramdom1000txt" to the location of your random number pool file, the number of spaces in "" to the number of spaces you use.

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.