以下是网上关于随机数生成的一类说法:
在计算机上可以用物理方法来产生随机数,但价格昂贵,不能重复,使用不便。另一种方法是用数学递推公式产生,这样产生的序列与真正的随机数序列不同,所以称为伪随机数或伪随机序列,只要方法和参数选择合适,所产生的伪随机数就能满足均匀性和独立性,与真正的随机数具有相近的性质。
以下是一个使用了线性同余的递推公式:
Xt = (X0 * 17 + 29) mod 500
线性同余中的线性,是指“线性”表示方程中 x 的次数是一次,mod 取余运算符则体现了“同余”这一数学概念。
式中,17 、29和500分别称做乘数、增量和模数。使用线性同余生成随机数的方法速度快,但对乘数、增量和模数的选取有一定的要求:
- 多次使用线性同余公式产生的序列应该看起来是随机的,不循环的;
- 乘数/增量与模数互质;
- 这个函数能够产生一个完整周期内的所有随机数。这一要求由模数控制。
<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <ctime></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <iostream></span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> MyRand { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span>: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">unsigned</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> seed; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 默认使用系统时间为种子</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// time(NULL) 返回从1970年元旦午夜0点到现在的秒数</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> srand(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">unsigned</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> s = (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">unsigned</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>)time(NULL)) { seed = s; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 使用了一种线性同余法,得到的随机数最大为(2^15-1),29为质数中的一个</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">unsigned</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> rand() { seed = (seed * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">31</span> + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">13</span>) % ((<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> << <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>) - <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> seed; } }; </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li></ul>
<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include "rand.h"</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main() { MyRand a; a.srand(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 使用系统时间为种子</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"产生若干个随机数:"</span> << <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::endl; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>; i++) <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << a.rand() % <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" "</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 生成0~100之间的随机数</span> getchar(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul>
使用错误的公式,得到的序列并不随机:
http://huisheng.fm/user/view/25853
http://huisheng.fm/user/view/25853
http://huisheng.fm/user/view/25855
http://huisheng.fm/user/view/25856
http://huisheng.fm/user/view/25857
http://huisheng.fm/user/view/25858
http://huisheng.fm/user/view/25859
http://huisheng.fm/user/view/25860
http://huisheng.fm/user/view/25861
http://huisheng.fm/user/view/25862
http://huisheng.fm/user/view/25863
http://huisheng.fm/user/view/25864
http://huisheng.fm/user/view/25865
http://huisheng.fm/user/view/25866
http://huisheng.fm/user/view/25867
http://huisheng.fm/user/view/25868
http://huisheng.fm/user/view/25869
http://huisheng.fm/user/view/25870
http://huisheng.fm/user/view/25871
http://huisheng.fm/user/view/25872
http://huisheng.fm/user/view/25873
http://huisheng.fm/user/view/25874
http://huisheng.fm/user/view/25875
http://huisheng.fm/user/view/25876
http://huisheng.fm/user/view/25877
http://huisheng.fm/user/view/25878
http://huisheng.fm/user/view/25879
http://huisheng.fm/user/view/25880
http://huisheng.fm/user/view/25881
http://huisheng.fm/user/view/25882
http://huisheng.fm/user/view/25883
http://huisheng.fm/user/view/25884
http://huisheng.fm/user/view/25885
http://huisheng.fm/user/view/25886
http://huisheng.fm/user/view/25887
http://huisheng.fm/user/view/25888
http://huisheng.fm/user/view/25889
http://huisheng.fm/user/view/25890
http://huisheng.fm/user/view/25891
http://huisheng.fm/user/view/25892
http://huisheng.fm/user/view/25893
http://huisheng.fm/user/view/25894
http://huisheng.fm/user/view/25895
http://huisheng.fm/user/view/25896
http://huisheng.fm/user/view/25897
http://huisheng.fm/user/view/25898
http://huisheng.fm/user/view/25899
http://huisheng.fm/user/view/25900
http://huisheng.fm/user/view/25901
http://huisheng.fm/user/view/25902
http://huisheng.fm/user/view/25903
http://huisheng.fm/user/view/25904
http://huisheng.fm/user/view/25905
http://huisheng.fm/user/view/25906
http://huisheng.fm/user/view/25907
http://huisheng.fm/user/view/25908
http://huisheng.fm/user/view/25909
http://huisheng.fm/user/view/25910
http://huisheng.fm/user/view/25911
http://huisheng.fm/user/view/25912
http://huisheng.fm/user/view/25913
http://huisheng.fm/user/view/25914
http://huisheng.fm/user/view/25915
http://huisheng.fm/user/view/25916
http://huisheng.fm/user/view/25917
http://huisheng.fm/user/view/25918
http://huisheng.fm/user/view/25919
http://huisheng.fm/user/view/25920
http://huisheng.fm/user/view/25921
http://huisheng.fm/user/view/25922
http://huisheng.fm/user/view/25923
http://huisheng.fm/user/view/25924
http://huisheng.fm/user/view/25925
http://huisheng.fm/user/view/25926
http://huisheng.fm/user/view/25927
http://huisheng.fm/user/view/25928
http://huisheng.fm/user/view/25929
http://huisheng.fm/user/view/25930
http://huisheng.fm/user/view/25931
http://huisheng.fm/user/view/25932
http://huisheng.fm/user/view/25933
http://huisheng.fm/user/view/25934
http://huisheng.fm/user/view/25935
http://huisheng.fm/user/view/25936
http://huisheng.fm/user/view/25937
http://huisheng.fm/user/view/25938
http://huisheng.fm/user/view/25939
http://huisheng.fm/user/view/25940
http://huisheng.fm/user/view/25941
http://huisheng.fm/user/view/25942
http://huisheng.fm/user/view/25943
http://huisheng.fm/user/view/25944
http://huisheng.fm/user/view/25945
http://huisheng.fm/user/view/25946
http://huisheng.fm/user/view/25947
http://huisheng.fm/user/view/25948
http://huisheng.fm/user/view/25949
http://huisheng.fm/user/view/25950
http://huisheng.fm/user/view/25951
http://huisheng.fm/user/view/25952
http://huisheng.fm/user/view/25953
http://huisheng.fm/user/view/25954
http://huisheng.fm/user/view/25955
http://huisheng.fm/user/view/25956
http://huisheng.fm/user/view/25957
http://huisheng.fm/user/view/25958
http://huisheng.fm/user/view/25959
http://huisheng.fm/user/view/25960
http://huisheng.fm/user/view/25961
http://huisheng.fm/user/view/25962
http://huisheng.fm/user/view/25963
http://huisheng.fm/user/view/25964
http://huisheng.fm/user/view/25965
http://huisheng.fm/user/view/25966
http://huisheng.fm/user/view/25967
http://huisheng.fm/user/view/25968
http://huisheng.fm/user/view/25969
http://huisheng.fm/user/view/25970
http://huisheng.fm/user/view/25971
http://huisheng.fm/user/view/25972
http://huisheng.fm/user/view/25973
http://huisheng.fm/user/view/25974
http://huisheng.fm/user/view/25975
http://huisheng.fm/user/view/25976
http://huisheng.fm/user/view/25977
http://huisheng.fm/user/view/25978
http://huisheng.fm/user/view/25979
http://huisheng.fm/user/view/25980
http://huisheng.fm/user/view/25981
http://huisheng.fm/user/view/25982
http://huisheng.fm/user/view/25983
http://huisheng.fm/user/view/25984
http://huisheng.fm/user/view/25985
http://huisheng.fm/user/view/25986
http://huisheng.fm/user/view/25987
http://huisheng.fm/user/view/25988
http://huisheng.fm/user/view/25989
http://huisheng.fm/user/view/25990
http://huisheng.fm/user/view/25991
http://huisheng.fm/user/view/25992
http://huisheng.fm/user/view/25993
http://huisheng.fm/user/view/25994
http://huisheng.fm/user/view/25995
http://huisheng.fm/user/view/25996
http://huisheng.fm/user/view/25997
http://huisheng.fm/user/view/25998
http://huisheng.fm/user/view/25999
http://huisheng.fm/user/view/26000
http://huisheng.fm/user/view/26001
http://huisheng.fm/user/view/26002
http://huisheng.fm/user/view/26003
http://huisheng.fm/user/view/26004
http://huisheng.fm/user/view/26005
http://huisheng.fm/user/view/26006
http://huisheng.fm/user/view/26007
http://huisheng.fm/user/view/26008
http://huisheng.fm/user/view/26009
http://huisheng.fm/user/view/26010
http://huisheng.fm/user/view/26011
http://huisheng.fm/user/view/26012
http://huisheng.fm/user/view/26013
http://huisheng.fm/user/view/26014
http://huisheng.fm/user/view/26015
http://huisheng.fm/user/view/26016
http://huisheng.fm/user/view/26017
http://huisheng.fm/user/view/26018
http://huisheng.fm/user/view/26019
http://huisheng.fm/user/view/26020
http://huisheng.fm/user/view/26021
http://huisheng.fm/user/view/26022
http://huisheng.fm/user/view/26024
http://huisheng.fm/user/view/26025
http://huisheng.fm/user/view/26026
http://huisheng.fm/user/view/26027
http://huisheng.fm/user/view/26028
http://huisheng.fm/user/view/26029
http://huisheng.fm/user/view/26030
http://huisheng.fm/user/view/26031
http://huisheng.fm/user/view/26032
http://huisheng.fm/user/view/26033
http://huisheng.fm/user/view/26034
http://huisheng.fm/user/view/26035
http://huisheng.fm/user/view/26036
http://huisheng.fm/user/view/26037
http://huisheng.fm/user/view/26038
http://huisheng.fm/user/view/26039
http://huisheng.fm/user/view/26040
http://huisheng.fm/user/view/26041
http://huisheng.fm/user/view/26042
http://huisheng.fm/user/view/26043
http://huisheng.fm/user/view/26044
http://huisheng.fm/user/view/26045
http://huisheng.fm/user/view/26046
http://huisheng.fm/user/view/26047
http://huisheng.fm/user/view/26048
http://huisheng.fm/user/view/26049
http://huisheng.fm/user/view/26050
http://huisheng.fm/user/view/26051
http://huisheng.fm/user/view/26052
http://huisheng.fm/user/view/26053
http://huisheng.fm/user/view/26054
http://huisheng.fm/user/view/26055
http://huisheng.fm/user/view/26056
http://huisheng.fm/user/view/26057
http://huisheng.fm/user/view/26058
http://huisheng.fm/user/view/26059
http://huisheng.fm/user/view/26060
http://huisheng.fm/user/view/26061
http://huisheng.fm/user/view/26062
http://huisheng.fm/user/view/26063
http://huisheng.fm/user/view/26064
http://huisheng.fm/user/view/26065
http://huisheng.fm/user/view/26066
http://huisheng.fm/user/view/26067
http://huisheng.fm/user/view/26068
http://huisheng.fm/user/view/26069
http://huisheng.fm/user/view/26070
http://huisheng.fm/user/view/26071
http://huisheng.fm/user/view/26072
http://huisheng.fm/user/view/26073
http://huisheng.fm/user/view/26074
http://huisheng.fm/user/view/26075
http://huisheng.fm/user/view/26076
http://huisheng.fm/user/view/26077
http://huisheng.fm/user/view/26078
http://huisheng.fm/user/view/26079
http://huisheng.fm/user/view/26080
http://huisheng.fm/user/view/26081
http://huisheng.fm/user/view/26082
http://huisheng.fm/user/view/26083
http://huisheng.fm/user/view/26084
http://huisheng.fm/user/view/26085
http://huisheng.fm/user/view/26086
http://huisheng.fm/user/view/26087
http://huisheng.fm/user/view/26088
http://huisheng.fm/user/view/26089
http://huisheng.fm/user/view/26090
http://huisheng.fm/user/view/26091
http://huisheng.fm/user/view/26092
http://huisheng.fm/user/view/26093
http://huisheng.fm/user/view/26094
http://huisheng.fm/user/view/26095
http://huisheng.fm/user/view/26096
http://huisheng.fm/user/view/26097
http://huisheng.fm/user/view/26098
http://huisheng.fm/user/view/26099
http://huisheng.fm/user/view/26100
http://huisheng.fm/user/view/26101
http://huisheng.fm/user/view/26102
http://huisheng.fm/user/view/26103
http://huisheng.fm/user/view/26104
http://huisheng.fm/user/view/26105
http://huisheng.fm/user/view/26106
http://huisheng.fm/user/view/26107
http://huisheng.fm/user/view/26108
http://huisheng.fm/user/view/26109
http://huisheng.fm/user/view/26110
http://huisheng.fm/user/view/26111
http://huisheng.fm/user/view/26112
http://huisheng.fm/user/view/26113
http://huisheng.fm/user/view/26114
http://huisheng.fm/user/view/26115
http://huisheng.fm/user/view/26116
http://huisheng.fm/user/view/26117
http://huisheng.fm/user/view/26118
http://huisheng.fm/user/view/26119
http://huisheng.fm/user/view/26120
http://huisheng.fm/user/view/26121
http://huisheng.fm/user/view/26122
http://huisheng.fm/user/view/26123
http://huisheng.fm/user/view/26124
http://huisheng.fm/user/view/26125
http://huisheng.fm/user/view/26126
http://huisheng.fm/user/view/26127
http://huisheng.fm/user/view/26128
http://huisheng.fm/user/view/26129
http://huisheng.fm/user/view/26130
http://huisheng.fm/user/view/26131
http://huisheng.fm/user/view/26132
http://huisheng.fm/user/view/26133
http://huisheng.fm/user/view/26134
http://huisheng.fm/user/view/26135
http://huisheng.fm/user/view/26136
http://huisheng.fm/user/view/26137
http://huisheng.fm/user/view/26138
http://huisheng.fm/user/view/26139
http://huisheng.fm/user/view/26140
http://huisheng.fm/user/view/26141
http://huisheng.fm/user/view/26142
http://huisheng.fm/user/view/26143
http://huisheng.fm/user/view/26144
http://huisheng.fm/user/view/26145
http://huisheng.fm/user/view/26146
http://huisheng.fm/user/view/26147
http://huisheng.fm/user/view/26148
http://huisheng.fm/user/view/26149
http://huisheng.fm/user/view/26150
http://huisheng.fm/user/view/26151
http://huisheng.fm/user/view/26152
http://huisheng.fm/user/view/26153
http://huisheng.fm/user/view/26154
http://huisheng.fm/user/view/26155
http://huisheng.fm/user/view/26156
http://huisheng.fm/user/view/26157
http://huisheng.fm/user/view/26158
http://huisheng.fm/user/view/26159
http://huisheng.fm/user/view/26160
http://huisheng.fm/user/view/26161
版权声明:本文为博主原创文章,未经博主允许不得转载。