File: randomnumber.h.html

package info (click to toggle)
rudiments 0.31-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 7,516 kB
  • ctags: 3,248
  • sloc: asm: 23,776; cpp: 22,792; sh: 7,769; ansic: 1,769; makefile: 1,054; xml: 169; perl: 19
file content (92 lines) | stat: -rw-r--r-- 6,550 bytes parent folder | download | duplicates (2)
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
<html>
<head>
<title>~/src/firstworks/rudiments-0.31/include/rudiments/randomnumber.h.html</title>
<meta name="Generator" content="Vim/7.0">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body bgcolor="#ffffff" text="#000000">
<pre>
<font color="#0000ff">// Copyright (c) 1999-2002 David Muse</font>
<font color="#0000ff">// See the COPYING file for more information.</font>

<font color="#a020f0">#ifndef RUDIMENTS_RANDOMNUMBER_H</font>
<font color="#a020f0">#define RUDIMENTS_RANDOMNUMBER_H</font>

<font color="#a020f0">#include </font><font color="#ff00ff">&lt;rudiments/private/randomnumberincludes.h&gt;</font>

<font color="#0000ff">// The randomnumber class provides methods for generating and scaling </font>
<font color="#0000ff">// random numbers.</font>
<font color="#0000ff">//</font>
<font color="#0000ff">// A common practice for generating random number sequences is to seed the</font>
<font color="#0000ff">// first call to generateNumber() with the number of seconds since 1970 (see</font>
<font color="#0000ff">// the datetime class) and seed each successive call with the previously</font>
<font color="#0000ff">// generated number.</font>
<font color="#0000ff">//</font>
<font color="#0000ff">// Superfluous background:</font>
<font color="#0000ff">//</font>
<font color="#0000ff">// Random numbers are actually just a highly divergent series.  There is</font>
<font color="#0000ff">// no such thing as true randomness.  Using the above described methodology, </font>
<font color="#0000ff">// if there is only one running process generating random numbers, successive </font>
<font color="#0000ff">// calls to generateNumber() will never return the same value until all </font>
<font color="#0000ff">// numbers between 0 and 2^32 have been returned.  At that point, the entire </font>
<font color="#0000ff">// sequence will repeat.  Calls to generateScaledNumber() may return the</font>
<font color="#0000ff">// same value before all numbers in the range have been returned because</font>
<font color="#0000ff">// it scales the result of generateNumber() which operates on a larger range.</font>

<font color="#a020f0">#ifdef RUDIMENTS_NAMESPACE</font>
<font color="#2e8b57"><b>namespace</b></font> rudiments {
<font color="#a020f0">#endif</font>

<font color="#2e8b57"><b>class</b></font> randomnumber {
        <font color="#a52a2a"><b>public</b></font>:
                <font color="#2e8b57"><b>static</b></font>        <font color="#2e8b57"><b>int</b></font>    generateNumber(<font color="#2e8b57"><b>int</b></font> seed);
                        <font color="#0000ff">// Generates a random number between 0 and 2^32, based</font>
                        <font color="#0000ff">// on seed, and returns it.  </font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// It is ok to use the result of this method as the </font>
                        <font color="#0000ff">// seed for the next number.</font>
                <font color="#2e8b57"><b>static</b></font>        <font color="#2e8b57"><b>int</b></font>    generateScaledNumber(<font color="#2e8b57"><b>int</b></font> seed,
                                                        <font color="#2e8b57"><b>int</b></font> lower, <font color="#2e8b57"><b>int</b></font> upper);
                        <font color="#0000ff">// Generates a random number between 0 and 2^32, based</font>
                        <font color="#0000ff">// on seed, scales that value to be between</font>
                        <font color="#0000ff">// lower and upper and returns it.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// It is NOT ok to use the result of this method as </font>
                        <font color="#0000ff">// the seed for the next number.</font>
                <font color="#2e8b57"><b>static</b></font>        <font color="#2e8b57"><b>int</b></font>    scaleNumber(<font color="#2e8b57"><b>int</b></font> number, <font color="#2e8b57"><b>int</b></font> lower, <font color="#2e8b57"><b>int</b></font> upper);
                        <font color="#0000ff">// Scales number to be between lower and upper and </font>
                        <font color="#0000ff">// returns it.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// It is NOT ok to use the result of this method as </font>
                        <font color="#0000ff">// the seed for the next number.</font>

                <font color="#2e8b57"><b>static</b></font>        <font color="#2e8b57"><b>int</b></font>    getRandMax();
                        <font color="#0000ff">// Operating systems can generate random numbers</font>
                        <font color="#0000ff">// between 0 and RAND_MAX.</font>
                        <font color="#0000ff">// This method returns RAND_MAX.</font>
                        <font color="#0000ff">// </font>
                        <font color="#0000ff">// RAND_MAX is usually 2^31-1 on 32 bit machines</font>
                        <font color="#0000ff">// but there are exceptions where it is 2^15-1 or</font>
                        <font color="#0000ff">// 2^32-1.</font>

                <font color="#2e8b57"><b>static</b></font>        <font color="#2e8b57"><b>bool</b></font>   needsMutex();
                        <font color="#0000ff">// If your system doesn't support rand_r() then this</font>
                        <font color="#0000ff">// class needs a mutex to assure thread safety.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// This method returns true if this class needs a mutex</font>
                        <font color="#0000ff">// to operate safely in a threaded environment and false</font>
                        <font color="#0000ff">// otherwise.</font>
                <font color="#2e8b57"><b>static</b></font>        <font color="#2e8b57"><b>void</b></font>   setMutex(mutex *mtx);
                        <font color="#0000ff">// Allows you to supply a mutex is the class needs it.</font>
                        <font color="#0000ff">// If your application is not multithreaded, then</font>
                        <font color="#0000ff">// there is no need to supply a mutex.</font>
};

<font color="#a020f0">#ifdef RUDIMENTS_NAMESPACE</font>
}
<font color="#a020f0">#endif</font>

<font color="#a020f0">#endif</font>
</pre>
</body>
</html>