zoukankan      html  css  js  c++  java
  • 生命游戏和随机数之间某种不可言说的秘密

    为什么我感觉随便写一个一维生命游戏规则就可以做出一个看起来很随机实际上也蛮随机的随机数生成器....

    这是代码:

     1 #include <cstdio>
     2 #include <fstream>
     3 #include <iostream>
     4  
     5 #include <cstdlib>
     6 #include <cstring>
     7 #include <algorithm>
     8 #include <cmath>
     9  
    10 #include <queue>
    11 #include <vector>
    12 #include <map>
    13 #include <set>
    14 #include <stack>
    15 #include <list>
    16  
    17 typedef unsigned int uint;
    18 typedef long long int ll;
    19 typedef unsigned long long int ull;
    20 typedef double db;
    21  
    22 using namespace std;
    23  
    24 inline int getint()
    25 {
    26     int res=0;
    27     char c=getchar();
    28     bool mi=false;
    29     while(c<'0' || c>'9') mi=(c=='-'),c=getchar();
    30     while('0'<=c && c<='9') res=res*10+c-'0',c=getchar();
    31     return mi ? -res : res;
    32 }
    33 inline ll getll()
    34 {
    35     ll res=0;
    36     char c=getchar();
    37     bool mi=false;
    38     while(c<'0' || c>'9') mi=(c=='-'),c=getchar();
    39     while('0'<=c && c<='9') res=res*10+c-'0',c=getchar();
    40     return mi ? -res : res;
    41 }
    42  
    43 db eps=1e-80;
    44 inline bool feq(db a,db b)
    45 { return fabs(a-b)<eps; }
    46 
    47 template<typename Type>
    48 inline Type avg(const Type a,const Type b)
    49 { return a+((b-a)/2); }
    50 
    51 //==============================================================================
    52 //==============================================================================
    53 //==============================================================================
    54 //==============================================================================
    55 
    56 int n,m;
    57 
    58 int a[2][1005000];
    59 
    60 bool rd[1005000];
    61 int rt=0;
    62 int lastrand=7;
    63 int getrand() 
    64 {
    65     int res=0;
    66     for(int i=0;i<10;i++) res+=(rd[lastrand+(i<<1)]<<i);
    67     lastrand++;
    68     return res;
    69 }
    70 
    71 
    72 
    73 int main()
    74 {
    75     freopen("in.txt","r",stdin);
    76     freopen("out.txt","w",stdout);
    77     
    78     n=getint();
    79     
    80     int mid=n*2,s=0;
    81     a[0][mid]=1;
    82     for(int i=1;i<n;i++)
    83     {
    84         for(int j=mid-i;j<=mid+i;j++)
    85         a[!s][j]=a[s][j]+a[s][j-2]^a[s][j+1]-a[s][j]^a[s][j+1]+a[s][j+1];
    86         s^=1;
    87         
    88         for(int j=mid-i;j<=mid+i;j++) rd[rt++]=(a[s][j]<0);
    89     }
    90     
    91     for(int i=0;i<12500;i++) cout<<getrand()<<',';
    92     
    93     
    94     return 0;
    95 }
    View Code

    种子可以设lastrand,也可以设lastrand的增量,甚至是要取哪些数据计算随机数...

    下面是按照以上代码生成0到1023的随机数12500个.

    看起来左边有点多?....不过应该也足够....随机了.....

    接下来的任务是给它写一个能用的class.....

    allocate之类的最讨厌了囧...

    然后下边是有趣的东西.....

    这是在cout<<getrand()后加了%611的结果.这告诉我们千万不要对原生随机数乱取模!......

    但是我就想要0到611之间的随机数呢?

    呃....把原区间用double对应到[0,1]范围内再乘回去似乎是个可行的办法....

    看起来常数有点大,我做梦的时候想想有没有更好的......

  • 相关阅读:
    错误 2 error C2059: 语法错误:“::”
    完全卸载session 所需要的函数
    header("Location:http://www.baidu.com");
    php str_pad() 用法
    php str_pad();
    设计模式系列-01-开篇
    博客园样式的设置系列-01-侧边栏和皮肤的设置
    vs20132015UML系列之-类图
    php获取当前时间和转换格式
    saltstack:multi-master configuration
  • 原文地址:https://www.cnblogs.com/DragoonKiller/p/4321070.html
Copyright © 2011-2022 走看看