C Basic Search for random function of G-point detailed _c language

Source: Internet
Author: User
Tags assert rand

Introduction

Random function algorithm should be one of the most important algorithms in the history of computer. and the random function used in C

#include <stdlib.h>

_check_return_ _acrtimp int __cdecl rand (void); 

This paper finds the G-spot mainly around the RAND function. is the periodic value of the pseudo random function.

About the Rand source code, you can find it from the Linux bottom source glibc. Took a look at about 4 files. The algorithm is more complicated. Feel very stable.

The implementation of stochastic algorithms is not discussed here. Just to find the random function cycle.

Objective

Now test on window. Test Code MAIN.C

#include <stdio.h> #include <stdlib.h> #define _INT_R (128) #define _INT_FZ (10000000)//Receive RAND () return
  return value and write to the file in the int getrand (long long *pcut) {static int _cut = 0;

  Long Long T = *pcut + 1;
  
  int r = rand ();
  
  Every time I go to the million to remind you if (t% _INT_FZ = 0) fprintf (stdout, "%d data ran out [%d,%lld]\n", _int_fz, _cut, T);
    if (T < 0) {//data exceeded ++_cut;
    fprintf (stderr, "now%d T >%lld\n", _cut, t-1); *pcut = 0;
  Re-start round} *pcut = t;
return R;
  }/* Verify the period of the RAND function */int main (int argc, char* argv[]) {int rbase[_int_r];
  int i =-1, R;

  
  Long long cut = 0;
  
  First a random function is generated while (++i < _int_r) Rbase[i] = Getrand (&cut); Here begins a 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;
      //See data if (i = = _int_r) {printf ("Now T =%lld\n", cut);
    Break
  
  }
  }System ("pause");
return 0; }

The main idea is to _int_r 128-digit overlap so we think. This cycle has been found.

The screenshot of the test results is

The main use of release X64 compiled. To verify that the above results are acceptable, change the _int_r to 1024 and recompile once.

The results of the operation are as follows:

In combination, we've seen the G-point of the RAND function on window.

2147483776-128 = 214748248

2147484672-1024 = 2147483648

Thus the Rand G point, which gets the VS2015 compiler on the window, is 2147483648.

The G-spot is used a lot in the game. such as lottery, off equipment, crit, and so on.

Body

1. Try the water on Linux

Try the test code on Linux basically the same rand2.c as follows

#include <stdio.h> #include <stdlib.h> #define _INT_R (1024) #define _INT_FZ (100000000)//Get Rand ()
  Returns a value and writes to a file of int getrand (long long *pcut) {static int _cut = 0;

  Long Long T = *pcut + 1;
  
  int r = rand ();
  
  Every time you remind me if (t% _INT_FZ = 0) fprintf (stdout, "%d data ran out again [%d,%lld]\n", _int_fz, _cut, T);
    if (T < 0) {//data exceeded ++_cut;
    fprintf (stderr, "now%d T >%lld\n", _cut, t-1); *pcut = 0;
  Re-start round} *pcut = t;
return R;
  }/* Verify the period of the RAND function */int main (int argc, char* argv[]) {int rbase[_int_r];
  int i =-1, R;

  
  Long long cut = 0;
  
  First a random function is generated while (++i < _int_r) Rbase[i] = Getrand (&cut); Here begins a 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;
      //See data if (i = = _int_r) {printf ("Now T =%lld\n", cut);
    Break
  
 }
  } return 0; }

Compile command

Gcc-03-o Randc2.out rand2.c

Finally run the result, wait for a long time still did not come out.

The RAND function on Linux is written in a very standard, random distribution. In short, this random value is relatively large. But there must be.

Interested can be optimized according to the above ideas run a run. Ubuntu is a slow virtual machine to run on this side.

2. Continue to expand to reduce Rand's return to Max. Try water.

Modify the Getrand function above

_int_rmax represents the range of random numbers [0)
#define _INT_RMAX
#define _INT_R    (1024)
#define _INT_FZ    ( 10000000)


//Get rand () return value and write to file
int Getrand (Long long *pcut) {
  static int _cut = 0;
  Long Long T = *pcut + 1;

  int r = rand ()% _int_rmax;

  Every time I go to the million to remind you
  if (t% _INT_FZ = 0)
    fprintf (stdout, "%d data ran out [%d,%lld]\n", _int_fz, _cut, T);

  if (T < 0) {//data exceeded
    ++_cut;
    fprintf (stderr, "now%d T >%lld\n", _cut, t-1);
    *pcut = 0; Re-start round
  }

  *pcut = t;
  return r;
}

Add the rest to see if it affects the G-spot test results

  

found that the G-spot did not change.

It can be inferred that the rand () cycle does not change with the two-time mod.

So you can rest assured that the MoD uses pseudo random functions. The G-spot is still that big.

3. Finally, give away a commonly used random function between [min, Max]

*
 * Returns the random function of the [min, max] Interval
 * min  : Starting position
 * Max  : End Position
 *    : Back to [Min, Max] position within the range
 * *
extern int random (int min, int max);

*
 * Returns the random function of the [min, max] Interval
 * min  : Start position
 * Max  : End Position
 *    : Return to position within [min, Max] Interval
 */< C24/>int 
Random (int min, int max) {
  assert (min < max);
  Normal return to
  rand ()% (Max-min + 1) + min;
}

The test demo code structure is as follows

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>

*
 * Returns the random function of the [min, max] Interval
 * min  : Start position
 * Max  : End Position
 *    : Return to position within [min, Max] Interval
 */< C13/>extern int random (int min, int max);
 * * C basis, using the random function
 *
/int main (int argc, char* argv[]) {

  int min = -5, max = 5;
  int i = 0;

  Begins the unified initialization of seed
  Srand ((unsigned) time (NULL));

  while (I < m) {
    printf ("%3d", Random (Min, max));
    if (++i% = 0)
      putchar (' \ n ');
  }

  System ("pause");
  return 0;
}

*
 * Returns the random function of the [min, max] Interval
 * min  : Start position
 * Max  : End Position
 *    : Return to position within [min, Max] Interval
 */< C39/>int 
Random (int min, int max) {
  assert (min < max);
  Normal return to
  rand ()% (Max-min + 1) + min;
}

The test results are

Basically more stable. Everything is to be expected.

Summing up this paper to draw two inferences

A. Rand () pseudo-random function with a G-spot. and can find

B. The G-Spot does not change with the two-time mod.

Postscript

Mistakes are unavoidable, I wish a happy tomorrow ~ ~

The above C basic search for random function of the G-point is small to share all the content of the whole, hope to give you a reference, but also hope that we support the cloud habitat community.

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.