-
Notifications
You must be signed in to change notification settings - Fork 0
/
the-pitfall-of-using-rand-for-simulation.html
175 lines (154 loc) · 11 KB
/
the-pitfall-of-using-rand-for-simulation.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE html>
<html lang="en">
<head>
<title>The pitfall of using rand() for simulation - You don't need to prove this</title>
<link href="https://newptcai.github.io/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="You don't need to prove this Full Atom Feed" />
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="https://newptcai.github.io/theme/css/w3.css">
<link rel="stylesheet" type="text/css" href="https://newptcai.github.io/theme/css/style.css">
<link rel="stylesheet" type="text/css" href="https://newptcai.github.io/theme/css/jqcloud.css">
<link rel="stylesheet" type="text/css" href="https://newptcai.github.io/theme/css/all.min.css">
<link rel="stylesheet" type="text/css" href="https://newptcai.github.io/theme/css/shariff.min.css">
<link rel="stylesheet" type="text/css" href="https://newptcai.github.io/theme/css/pygments-highlight-github.css">
<!-- JavaScript -->
<script src="https://newptcai.github.io/theme/js/jquery-3.5.1.min.js"></script>
<script src="https://newptcai.github.io/theme/js/jqcloud.min.js"></script>
<!-- Meta -->
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="HandheldFriendly" content="True" />
<meta name="author" content="Xing Shi Cai" />
<meta name="description" content="This was probably the first simulation I did. How naive I was!" />
<meta name="keywords" content="probability, programming, experimental-math">
<!-- Facebook OpenGraph -->
<meta property="og:site_name" content="You don't need to prove this">
<meta property="og:title" content="The pitfall of using rand() for simulation - You don't need to prove this" />
<meta property="og:description" content="This was probably the first simulation I did. How naive I was!" />
<meta property="og:image" content="https://newptcai.github.io">
<meta property="og:type" content="article" />
<meta property="og:url" content="https://newptcai.github.io/the-pitfall-of-using-rand-for-simulation.html" />
<meta property="og:locale" content="de_DE" />
<meta property="og:locale:alternate" content="en_US" />
<!-- Twitter -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="The pitfall of using rand() for simulation - You don't need to prove this">
<meta name="twitter:description" content="This was probably the first simulation I did. How naive I was!">
<meta name="twitter:image" content="https://newptcai.github.io">
</head>
<body>
<div class="w3-row w3-card w3-white">
<header id=banner>
<!-- AUTHOR INITIALS-->
<a href="https://newptcai.github.io" id=logo title="Home">XS</a>
<nav id="menu">
<ul>
<li><a href="https://newptcai.github.io/pages/research.html">Research</a></li>
<li><a href="https://newptcai.github.io/pages/teaching.html">Teaching</a></li>
<li class="active"><a href="https://newptcai.github.io/category/math.html">math</a></li>
<li ><a href="https://newptcai.github.io/category/mumble.html">mumble</a></li>
<li ><a href="https://newptcai.github.io/category/photo.html">photo</a></li>
</ul>
</nav>
</header>
</div>
<br>
<article>
<header class="w3-container col-main">
<h1>The pitfall of using rand() for simulation</h1>
<div class="post-info">
<div class="w3-opacity w3-margin-right w3-margin-bottom" style="flex-grow: 1;">
<span> Posted on Sat 22 January 2011 in <a href="https://newptcai.github.io/category/math.html" style="font-style: italic">math</a>
</span>
</div>
<div id="article-tags">
<span class="w3-tag w3-light-grey w3-text-red w3-hover-red">
<a href="https://newptcai.github.io/tag/probability.html" title=" All posts about Probability
">#probability</a>
</span>
<span class="w3-tag w3-light-grey w3-text-red w3-hover-red">
<a href="https://newptcai.github.io/tag/programming.html" title=" All posts about Programming
">#programming</a>
</span>
<span class="w3-tag w3-light-grey w3-text-red w3-hover-red">
<a href="https://newptcai.github.io/tag/experimental-math.html" title=" All posts about Experimental-Math
">#experimental-math</a>
</span>
</div>
</div>
</header>
<br>
<div class="col-main w3-container">
<main id="article-content">
<p><em>This was probably the first simulation I did. How naive I was!</em></p>
<p>Today I was doing some simulation and I need a uniform random bits stream.
What I tried first is to use the standard C function <a href="http://en.cppreference.com/w/c/numeric/random/rand"><code>rand()</code></a>, which should return a uniform integer
between <code>1</code> and <code>2^32</code>. So I thought I could just use <code>rand()</code> to generate an integer and use it as
32 uniform random bits. But then I noticed the behavior of my simulation is a bit off my
expectation. In the end I wrote <a href="../code/naive-bit-stream.c">the following program</a> to count how many 0's and 1's I get when I
use <code>rand()</code> many times.</p>
<p>{% highlight c %}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h></p>
<div class="highlight"><pre><span></span><code><span class="o">#</span><span class="n">define</span> <span class="n">INT_BITS</span> <span class="p">(</span><span class="mi">8</span> <span class="o">*</span> <span class="n">sizeof</span><span class="p">(</span><span class="nb">int</span><span class="p">))</span>
<span class="nb">int</span> <span class="n">main</span><span class="p">(</span><span class="n">void</span><span class="p">)</span>
<span class="err">{</span>
<span class="n">srand</span><span class="p">(</span><span class="k">time</span><span class="p">(</span><span class="mi">0</span><span class="p">));</span> <span class="o">//</span><span class="n">use</span> <span class="k">current</span> <span class="k">time</span> <span class="k">as</span> <span class="n">seed</span> <span class="k">for</span> <span class="n">random</span> <span class="n">generator</span>
<span class="nb">int</span> <span class="n">random_variable</span><span class="p">;</span>
<span class="n">unsigned</span> <span class="nb">char</span> <span class="nb">bit</span><span class="p">;</span>
<span class="n">unsigned</span> <span class="n">long</span> <span class="nb">int</span> <span class="n">k</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">total_bits</span><span class="p">,</span> <span class="n">zeros</span><span class="p">;</span>
<span class="n">double</span> <span class="n">zero_ratio</span><span class="p">;</span>
<span class="n">zeros</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="k">for</span><span class="p">(</span><span class="n">n</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">n</span> <span class="o"><</span> <span class="mi">1</span><span class="n">e7</span><span class="p">;</span> <span class="n">n</span><span class="o">++</span><span class="p">)</span>
<span class="err">{</span>
<span class="k">for</span><span class="p">(</span><span class="n">k</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">k</span> <span class="o"><</span> <span class="n">INT_BITS</span><span class="p">;</span> <span class="n">k</span><span class="o">++</span><span class="p">)</span>
<span class="err">{</span>
<span class="n">random_variable</span> <span class="o">=</span> <span class="n">rand</span><span class="p">();</span>
<span class="nb">bit</span> <span class="o">=</span> <span class="p">(</span><span class="n">random_variable</span> <span class="o">>></span> <span class="n">k</span><span class="p">)</span> <span class="o">&</span> <span class="mi">1</span><span class="p">;</span>
<span class="k">if</span><span class="p">(</span><span class="nb">bit</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
<span class="err">{</span>
<span class="n">zeros</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>
<span class="err">}</span>
<span class="err">}</span>
<span class="err">}</span>
<span class="n">total_bits</span> <span class="o">=</span> <span class="n">INT_BITS</span> <span class="o">*</span> <span class="n">n</span><span class="p">;</span>
<span class="n">zero_ratio</span> <span class="o">=</span> <span class="p">(</span><span class="nb">float</span><span class="p">)</span><span class="n">zeros</span><span class="o">/</span><span class="n">total_bits</span><span class="p">;</span>
<span class="o">//</span><span class="n">printf</span><span class="p">(</span><span class="ss">"RAND_MAX %d\n"</span><span class="p">,</span> <span class="n">RAND_MAX</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="ss">"total zeros %lu total bits %lu average %f\n"</span><span class="p">,</span> <span class="n">zeros</span><span class="p">,</span> <span class="n">total_bits</span><span class="p">,</span> <span class="n">zero_ratio</span><span class="p">);</span>
<span class="err">}</span>
</code></pre></div>
<p>{% endhighlight %}</p>
<p>The result is pretty clear on my machine.</p>
<div class="highlight"><pre><span></span><code><span class="err">total zeros 164992607 total bits 320000000 average 0.515602</span>
</code></pre></div>
<p>I consistently got more zeros than ones. And this bias does not vanish when I increased size.
In the end I switched to <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html">Mersenne
Twister</a> pseudo-random generator.
Works much better!</p>
</main>
<br>
<footer>
<div class="adjust-width">
<div id="author-block" class="w3-light-grey w3-border">
<img style="width: 35px; height: 56px; margin-left:50px;" src="https://newptcai.github.io/theme/images/bookmark-red.png" alt="bookmark"></img>
<div id="author-info">
<a href="https://newptcai.github.io/authors.html#xing-shi-cai"><img
style="width: 60px; height: 60px;" src="https://newptcai.github.io/authors/xing-shi-cai.png" onerror="this.src='https://newptcai.github.io/theme/images/avatar.png'"></img>
</a>
<div style="margin-left: 20px; margin-top: 15px;">
<a href="https://newptcai.github.io/authors.html#xing-shi-cai"><span id="author-name" class="w3-hover-text-dark-grey">Xing Shi Cai</span></a>
<p id="author-story" style="max-width: 500px;"></p>
</div>
</div>
</div>
</div>
<br>
</footer>
</div>
</article>
<br>
<script src="https://newptcai.github.io/theme/js/shariff.min.js"></script>
</body>
</html>