A random sequence algorithm written in C Language

Source: Internet
Author: User
Tags random seed

I have been engaged in databases for a long time and have never touched the C language. In order to train the operator, an incomplete algorithm that generates a random sequence without repeating was rewritten some time ago.

 

The C language does not have the concept of Random Seed values, so it is not easy to generate a random and non-repeating sequence. The most common idea is to give a range and perform the rand () % MAX_VALUE operation. If this value has been obtained, a new value is generated. With this algorithm, you will find that when the value of MAX_VALUE reaches a certain level (my test value is 20), it will fall into an endless loop. The reason is that, for example, 20 numbers, taking the first 18 numbers, the probability of generating the next two numbers will be exponential and reduced, and hundreds of thousands of operations may be performed, nor can the last two numbers be generated. So I thought about an algorithm again.

Algorithm principle: two columns are given to generate a random number, copy the number at the position of the second series to the first series, and compress the second series, all values are used up.

First Iteration

 

Compress the temporary series for the second iteration:

 

In this way, an even and non-repeating random sequence can be generated. However, according to the test results, the random series generated each time for the same MAX_VALUE value is the same. Therefore, if this value is applied to production, additional processing is required.

The source code is as follows:

 

# Include "stdafx. h"

# Include <stdlib. h>

# Include <stdio. h>

# Include <memory. h>

 

Intrand_array (int * arr, int floor );

VoidMergArr (int * pArr, int nSize );

 

# DefineMAX_VALUE 10

 

Int main (intargc, char ** argv)

{

Int rand_list [MAX_VALUE];

Int ret = 0;

Int I = 0;

 

Ret = rand_array (rand_list, MAX_VALUE );

 

For (I = 0; I <MAX_VALUE; I ++)

{

Printf ("rand_list [% d]: % d \ r \ n", I, rand_list [I]);

}

 

Return 0;

}

 

Intrand_array (int * pArrList, int nFloor)

{

Int I = 0;

Int * pArrTmp = NULL; // temporary Array

Int nRand = 0;

Int nTotal = 0;

Int nLastSize = 0;

Int nMemSize = nFloor * sizeof (int) + 1;

 

// Allocate memory to the array first

PArrTmp = (int *) malloc (nMemSize );

 

If (pArrList = NULL | pArrTmp = NULL)

{

Return-1;

}

 

// Initialize the Array

For (I = 0; I <nFloor; I ++)

{

PArrList [I] =-1;

PArrTmp [I] = I;

}

 

NLastSize = nFloor;

 

// Generate the instant count and fill in the pArrTmp table to pArrList

While (nTotal <nFloor)

{

NRand = rand () % (nFloor-nTotal );

 

If (pArrTmp [nRand]! =-1)

{

PArrList [nTotal] = pArrTmp [nRand];

PArrTmp [nRand] =-1;

NTotal ++;

}

Else

{

MergArr (pArrTmp, nLastSize );

NLastSize = nFloor-nTotal;

}

}

 

Free (pArrTmp );

PArrTmp = NULL;

 

Return 0;

}

 

VoidMergArr (int * pArr, int nSize)

{

Int I = 0;

 

Int total = 0;

 

For (I = 0; I <nSize; I ++)

{

If (pArr [I]! =-1)

{

PArr [total] = pArr [I];

 

If (total! = I)

PArr [I] =-1;

 

Total ++;

}

 

}

}

Related Article

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.