C basic searches for random function g points, c basic Random Functions

Source: Internet
Author: User

C basic searches for random function g points, c basic Random Functions

Introduction

The random function algorithm should be one of the top ten most important algorithms in computer history. The random function used in C

#include <stdlib.h>_Check_return_ _ACRTIMP int __cdecl rand(void);

This article mainly finds the G point around the rand function, that is, the periodic value of the pseudo-random function.

For the rand source code, you can find the underlying Linux source code glibc. I checked about four files. The algorithm is complicated and stable.

The implementation of random algorithms is not discussed here.Random Function Period.

 

Preface

Test the main. c code on the window.

# Include <stdio. h> # include <stdlib. h> # define _ INT_R (128) # define _ INT_FZ (10000000) // get the returned value of rand () and write it to the file int getrand (long * pcut) {static int _ cut = 0; long t = * pcut + 1; int r = rand (); // remind if (t % _ INT_FZ = 0) fprintf (stdout, "% d data ran out [% d, % lld] \ n ", _ INT_FZ, _ cut, t); if (t <0) {// The data exceeds the threshold + + _ cut; fprintf (stderr, "Now % d T> % lld \ n", _ cut, t-1); * pcut = 0; // start a round again} * pcu T = t; return r;}/** verify the cycle of the rand function */int main (int argc, char * argv []) {int rbase [_ INT_R]; int I =-1, r; long cut = 0; // first generate the random function while (++ I <_ INT_R) rbase [I] = getrand (& cut ); // random for (;) {r = getrand (& cut); if (r! = Rbase [0]) continue; for (I = 1; I <_ INT_R; ++ I) {r = getrand (& cut); if (r! = Rbase [I]) break;} // you can find the data if (I = _ INT_R) {printf ("Now T = % lld \ n", cut ); break ;}} system ("pause"); return 0 ;}

The main idea is_ INT_RIf there are 128 overlapping numbers, we think we have found this cycle.

Test result:

 

Release X64 compilation is mainly used. To verify that the above results are acceptable, change _ INT_R to 1024 and re-compile it.

The running result is as follows:

 

Based on the above, we can see that the gpoint of the rand function on the window is

2147483776-128 = 214748248

2147484672-1024 = 2147483648

Therefore, the rand G point of the VS2015 compiler on the window is2147483648.

Gdian is used in many games, such as lottery drawing, equipment drop, and violent attacks.

 

Body

1. Try water on linux

In linux, try to test the Code as follows: rand2.c

# Include <stdio. h> # include <stdlib. h> # define _ INT_R (1024) # define _ INT_FZ (100000000) // get the returned value of rand () and write it to the file int getrand (long * pcut) {static int _ cut = 0; long t = * pcut + 1; int r = rand (); // remind me if (t % _ INT_FZ = 0) fprintf (stdout, "% d data ran out again [% d, % lld] \ n ", _ INT_FZ, _ cut, t); if (t <0) {// The data exceeds the standard ++ _ cut; fprintf (stderr, "Now % d T> % lld \ n", _ cut, t-1); * pcut = 0; // start a round again} * p Cut = t; return r;}/** verify the cycle of the rand function */int main (int argc, char * argv []) {int rbase [_ INT_R]; int I =-1, r; long cut = 0; // first generate the random function while (++ I <_ INT_R) rbase [I] = getrand (& cut ); // random for (;) {r = getrand (& cut); if (r! = Rbase [0]) continue; for (I = 1; I <_ INT_R; ++ I) {r = getrand (& cut); if (r! = Rbase [I]) break;} // you can find the data if (I = _ INT_R) {printf ("Now T = % lld \ n", cut ); break;} return 0 ;}

Compile command

gcc -03 -o randc2.out rand2.c

The final running result is not displayed after waiting for a long time.

 

The rand function on Linux is very well written and distributed randomly. In short, this random value is relatively large, but it must exist.

If you are interested, you can follow the above ideas to optimize the run. Ubuntu is slow for virtual machines.

 

2. continue expansion, reduce the rand return the MAX value, and try the water.

Modify the above getrand Function

// _ INT_RMAX indicates the random number range [0,100) # define _ INT_RMAX (100) # define _ INT_R (1024) # define _ INT_FZ (10000000) // obtain the return value of rand, and write it to the file int getrand (long * pcut) {static int _ cut = 0; long t = * pcut + 1; int r = rand () % _ INT_RMAX; // remind if (t % _ INT_FZ = 0) fprintf (stdout, "% d data ran out [% d, % lld] \ n ", _ INT_FZ, _ cut, t); if (t <0) {// The data exceeds the threshold + + _ cut; fprintf (stderr, "Now % d T> % lld \ n", _ cut, t-1); * pcut = 0; // start a round again} * pcut = t; return r ;}

 

Added the remainder check to check whether the gpoint test result is affected.

  

It is found that the G point has not changed.

There can be Inferences:The rand () cycle does not change with the remainder of the second mod.

Therefore, we can rest assured that mod uses pseudo-random functions. The G point is still so large.

 

3. Finally, a common random function between [min, max] is presented.

/** Return the random function * min: Start position * max: end position *: return [min, max] position within the interval */extern int random (int min, int max);/** returns the random function of the [min, max] interval * min: Start position * max: end position *: returns the position within the [min, max] range */int random (int min, int max) {assert (min <max ); // return rand () % (max-min + 1) + min ;}

 

The demo code structure is as follows:

# Include <stdio. h> # include <stdlib. h> # include <time. h> # include <assert. h>/** return the random function * min: Start position * max: end position *: return [min, max] position within the interval */extern int random (int min, int max);/** C basis, using a random function */int main (int argc, char * argv []) {int min =-5, max = 5; int I = 0; // start unified initialization of seed srand (unsigned) time (NULL )); while (I <100) {printf ("% 3d", random (min, max); if (++ I % 10 = 0) putchar ('\ n') ;}system ("pause"); return 0 ;}/ ** returns a random function * min in the [min, max] interval: start position * max: end position *: returns the position within the [min, max] range */int random (int min, int max) {assert (min <max ); // return rand () % (max-min + 1) + min ;}

 

Test result:

Relatively stable. Everything is expected.

Two inferences are drawn from the conclusion.

A. rand () pseudo-random function, which has a gpoint and can be found.

B. The gpoint does not change with the remainder of the second mod.

 

Postscript

Errors are inevitable. I wish you a happy tomorrow ~~

  

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.