A simple algorithm for random problem extraction

Source: Internet
Author: User
Tags exit comparison current time rand repetition

Random problem is a lot of testing software often encountered problems, set up the relevant questions in the question bank, to extract the M (m<=n) problem, which will first produce m random number. In the C language, the general practice is:

int *intArray;
int i;
time_t t;
intArray = malloc(m*sizeof(int));
/*time(&t)将获取当前时间,srand把当前时间作为随机数的种子*/
srand((unsigned) time(&t));
/*依次产生m个随机数*/
for(i=0; i<m; i++)
  intArray[i] = rand() %n;
……
free(intArray);
In this way, we can produce m random number, the method is very simple, and using the current time as a random number of seeds, as far as possible to avoid the occurrence of repeated problems. But careful analysis, repeated extraction is not completely avoided, and whether the problem has not affected the future extraction, will lead to the probability of each question is extracted. The correct method is to check whether the new extraction of the problem is repeated, if repeated, the method is very simple, only in the above program to add the sentence to judge the repetition, but the probability of each question is still different. What do we do?

We can look at the number of n from 1 to n as a circle of n individuals, generate a random number round, starting from 1 (more than n has to be 1), when the number to round, round exit (after the number of round will be skipped), and then produce a random number round1, From the front of the round has been counted to the round1 (in descending order, if passed round will skip), ..., and so on, until m questions are extracted.

This method looks very difficult on the surface, to set a set of N-ary, the elements that have been counted will be deleted until the M element is extracted so that there is a set of N (General N>>m) elements that will consume more time and space resources. Is there a simpler way?

First, analyze the impact of "exit". Round exit, less than the number of round, greater than the number of round minus one; after the Round1 exit, the number of less than round1 is not changed, is greater than the number of round1 and also reduce A; This can be very simple to analyze a simple algorithm: still according to the method mentioned above to extract the random number ROUNDK, the ROUNDK by N for the remainder, and then the ROUNDK and Round1, Round2, ..., roundk-1 (this k-1 number has been added order, Roundk-1 for the first k-1 to get the largest number of random numbers, and then enter the comparison program, first and round1 comparison, if roundk>= round1, then ROUNDK, and then compared with Round2, if roundk>= round2, Then roundk one more, ..., so that we can easily achieve without repetition and each question is extracted the same probability of random extraction algorithm. The specific approach is:int *intArray;
int i,j,k,temp;
time_t t;
intArray = malloc(m*sizeof(int));
srand((unsigned) time(&t));
/*依次产生m个随机数*/
for(i=0; i<m; i++){
  temp= rand() %n;
  /*查找temp原先的“真实”编号*/
  for(j=0; j<i; j++)
  if(temp>= intArray[j])
  temp++;
  else{
  /*temp应插在k位置处, 这样数组intArray就实现了排序,同时得到了temp原先的编号*/
  k=j-1;
  break;
  }
  for(j=i-1;j>k;j--)
    intArray [j+1]= intArray [j];
  intArray [k] =temp; ①
/*以下根据题号产生题库部分省略*/
……
}
free(intArray);
The advantage of the above practice is that there is no additional storage space, the complexity of the operation is roughly equal to an insert sort algorithm, but the original generated title sequence has been "ignored", add an additional array with M elements, you can retain the original order of the number of titles, For example, Intrandarray is an additional array with M elements, and the ① is changed to:intRandArray[i] =intArray [k]= temp;So we can have a very small time and space cost, realize without repetition and each question is extracted the same probability of random problem extraction algorithm. But in C language rand () has been suffering from scale disease, professionals have been looking for a better random number generation algorithm, there are many reference materials, please consult the reader, this article no longer repeat. The reader may consider to integrate the random number generation algorithm into the random extraction algorithm in this paper, so as to obtain a better algorithm for random problem extraction.

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.