📄 glib-random-numbers.html
字号:
<html xmlns:cf="http://docbook.sourceforge.net/xmlns/chunkfast/1.0"><head><meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"><title>Random Numbers</title><meta name="generator" content="DocBook XSL Stylesheets V1.69.0"><link rel="start" href="index.html" title="GLib Reference Manual"><link rel="up" href="glib-utilities.html" title="GLib Utilities"><link rel="prev" href="glib-Date-and-Time-Functions.html" title="Date and Time Functions"><link rel="next" href="glib-Hook-Functions.html" title="Hook Functions"><meta name="generator" content="GTK-Doc V1.4 (XML mode)"><link rel="stylesheet" href="style.css" type="text/css"><link rel="chapter" href="glib.html" title="GLib Overview"><link rel="chapter" href="glib-fundamentals.html" title="GLib Fundamentals"><link rel="chapter" href="glib-core.html" title="GLib Core Application Support"><link rel="chapter" href="glib-utilities.html" title="GLib Utilities"><link rel="chapter" href="glib-data-types.html" title="GLib Data Types"><link rel="chapter" href="tools.html" title="GLib Tools"><link rel="index" href="ix01.html" title="Index"><link rel="index" href="ix02.html" title="Index of deprecated symbols"><link rel="index" href="ix03.html" title="Index of new symbols in 2.2"><link rel="index" href="ix04.html" title="Index of new symbols in 2.4"><link rel="index" href="ix05.html" title="Index of new symbols in 2.6"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle"><td><a accesskey="p" href="glib-Date-and-Time-Functions.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td><td><a accesskey="u" href="glib-utilities.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td><td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td><th width="100%" align="center">GLib Reference Manual</th><td><a accesskey="n" href="glib-Hook-Functions.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td></tr></table><div class="refentry" lang="en"><a name="glib-Random-Numbers"></a><div class="titlepage"></div><div class="refnamediv"><table width="100%"><tr><td valign="top"><h2><span class="refentrytitle">Random Numbers</span></h2><p>Random Numbers — pseudo-random number generator.</p></td><td valign="top" align="right"></td></tr></table></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">#include <glib.h> <a href="glib-Random-Numbers.html#GRand">GRand</a>;<a href="glib-Random-Numbers.html#GRand">GRand</a>* <a href="glib-Random-Numbers.html#g-rand-new-with-seed">g_rand_new_with_seed</a> (<a href="glib-Basic-Types.html#guint32">guint32</a> seed);<a href="glib-Random-Numbers.html#GRand">GRand</a>* <a href="glib-Random-Numbers.html#g-rand-new-with-seed-array">g_rand_new_with_seed_array</a> (const <a href="glib-Basic-Types.html#guint32">guint32</a> *seed, <a href="glib-Basic-Types.html#guint">guint</a> seed_length);<a href="glib-Random-Numbers.html#GRand">GRand</a>* <a href="glib-Random-Numbers.html#g-rand-new">g_rand_new</a> (void);<a href="glib-Random-Numbers.html#GRand">GRand</a>* <a href="glib-Random-Numbers.html#g-rand-copy">g_rand_copy</a> (<a href="glib-Random-Numbers.html#GRand">GRand</a> *rand_);void <a href="glib-Random-Numbers.html#g-rand-free">g_rand_free</a> (<a href="glib-Random-Numbers.html#GRand">GRand</a> *rand_);void <a href="glib-Random-Numbers.html#g-rand-set-seed">g_rand_set_seed</a> (<a href="glib-Random-Numbers.html#GRand">GRand</a> *rand_, <a href="glib-Basic-Types.html#guint32">guint32</a> seed);void <a href="glib-Random-Numbers.html#g-rand-set-seed-array">g_rand_set_seed_array</a> (<a href="glib-Random-Numbers.html#GRand">GRand</a> *rand_, const <a href="glib-Basic-Types.html#guint32">guint32</a> *seed, <a href="glib-Basic-Types.html#guint">guint</a> seed_length);#define <a href="glib-Random-Numbers.html#g-rand-boolean">g_rand_boolean</a> (rand_)<a href="glib-Basic-Types.html#guint32">guint32</a> <a href="glib-Random-Numbers.html#g-rand-int">g_rand_int</a> (<a href="glib-Random-Numbers.html#GRand">GRand</a> *rand_);<a href="glib-Basic-Types.html#gint32">gint32</a> <a href="glib-Random-Numbers.html#g-rand-int-range">g_rand_int_range</a> (<a href="glib-Random-Numbers.html#GRand">GRand</a> *rand_, <a href="glib-Basic-Types.html#gint32">gint32</a> begin, <a href="glib-Basic-Types.html#gint32">gint32</a> end);<a href="glib-Basic-Types.html#gdouble">gdouble</a> <a href="glib-Random-Numbers.html#g-rand-double">g_rand_double</a> (<a href="glib-Random-Numbers.html#GRand">GRand</a> *rand_);<a href="glib-Basic-Types.html#gdouble">gdouble</a> <a href="glib-Random-Numbers.html#g-rand-double-range">g_rand_double_range</a> (<a href="glib-Random-Numbers.html#GRand">GRand</a> *rand_, <a href="glib-Basic-Types.html#gdouble">gdouble</a> begin, <a href="glib-Basic-Types.html#gdouble">gdouble</a> end);void <a href="glib-Random-Numbers.html#g-random-set-seed">g_random_set_seed</a> (<a href="glib-Basic-Types.html#guint32">guint32</a> seed);#define <a href="glib-Random-Numbers.html#g-random-boolean">g_random_boolean</a> ()<a href="glib-Basic-Types.html#guint32">guint32</a> <a href="glib-Random-Numbers.html#g-random-int">g_random_int</a> (void);<a href="glib-Basic-Types.html#gint32">gint32</a> <a href="glib-Random-Numbers.html#g-random-int-range">g_random_int_range</a> (<a href="glib-Basic-Types.html#gint32">gint32</a> begin, <a href="glib-Basic-Types.html#gint32">gint32</a> end);<a href="glib-Basic-Types.html#gdouble">gdouble</a> <a href="glib-Random-Numbers.html#g-random-double">g_random_double</a> (void);<a href="glib-Basic-Types.html#gdouble">gdouble</a> <a href="glib-Random-Numbers.html#g-random-double-range">g_random_double_range</a> (<a href="glib-Basic-Types.html#gdouble">gdouble</a> begin, <a href="glib-Basic-Types.html#gdouble">gdouble</a> end);</pre></div><div class="refsect1" lang="en"><a name="id3003141"></a><h2>Description</h2><p>The following functions allow you to use a portable, fast and goodpseudo-random number generator (PRNG). It uses the Mersenne TwisterPRNG, which was originally developed by Makoto Matsumoto and TakujiNishimura. Further information can be found at <a href="http://www.math.keio.ac.jp/~matumoto/emt.html" target="_top">www.math.keio.ac.jp/~matumoto/emt.html</a>.</p><p>If you just need a random number, you simply call the <code class="function">g_random_*</code> functions, which will create a globally used <a href="glib-Random-Numbers.html#GRand"><span class="type">GRand</span></a> and use the according <code class="function">g_rand_*</code> functions internally. Whenever you need a stream of reproducible random numbers, you better create a <a href="glib-Random-Numbers.html#GRand"><span class="type">GRand</span></a> yourself and use the <code class="function">g_rand_*</code> functions directly, which will also be slightly faster. Initializing a <a href="glib-Random-Numbers.html#GRand"><span class="type">GRand</span></a> with a certain seed will produce exactly the same series of random numbers on all platforms. This can thus be used as a seed for e.g. games.</p><p>The <code class="function">g_rand*_range</code> functions will return high qualityequally distributed random numbers, whereas for example the<code class="literal">(<a href="glib-Random-Numbers.html#g-random-int"><code class="function">g_random_int()</code></a>%max)</code> approach often doesn'tyield equally distributed numbers.</p><p>GLib changed the seeding algorithm for the pseudo-random numbergenerator Mersenne Twister, as used by <span class="structname">GRand</span>and <span class="structname">GRandom</span>. This was necessary, because someseeds would yield very bad pseudo-random streams. Also thepseudo-random integers generated by<code class="function">g_rand*<code class="function">_int_range()</code></code> will have aslightly better equal distribution with the new version of GLib.</p><p>The original seeding and generation algorithms, as found in GLib 2.0.x,can be used instead of the new ones by setting the environment variable<code class="envar">G_RANDOM_VERSION</code> to the value of '2.0'. Use theGLib-2.0 algorithms only if you have sequences of numbers generatedwith Glib-2.0 that you need to reproduce exactly.</p></div><div class="refsect1" lang="en"><a name="id3003287"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id3003292"></a><h3><a name="GRand"></a>GRand</h3><a class="indexterm" name="id3003302"></a><pre class="programlisting">typedef struct _GRand GRand;</pre><p>The <a href="glib-Random-Numbers.html#GRand"><span class="type">GRand</span></a> struct is an opaque data structure. It should only beaccessed through the <code class="function">g_rand_*</code> functions.</p></div><hr><div class="refsect2" lang="en"><a name="id3003333"></a><h3><a name="g-rand-new-with-seed"></a>g_rand_new_with_seed ()</h3><a class="indexterm" name="id3003344"></a><pre class="programlisting"><a href="glib-Random-Numbers.html#GRand">GRand</a>* g_rand_new_with_seed (<a href="glib-Basic-Types.html#guint32">guint32</a> seed);</pre><p>Creates a new random number generator initialized with <em class="parameter"><code>seed</code></em>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>seed</code></em> :</span></td><td> a value to initialize the random number generator.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td> the new <a href="glib-Random-Numbers.html#GRand"><span class="type">GRand</span></a>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3003417"></a><h3><a name="g-rand-new-with-seed-array"></a>g_rand_new_with_seed_array ()</h3><a class="indexterm" name="id3003429"></a><pre class="programlisting"><a href="glib-Random-Numbers.html#GRand">GRand</a>* g_rand_new_with_seed_array (const <a href="glib-Basic-Types.html#guint32">guint32</a> *seed, <a href="glib-Basic-Types.html#guint">guint</a> seed_length);</pre><p>Creates a new random number generator initialized with <em class="parameter"><code>seed</code></em>.</p><p></p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>seed</code></em> :</span></td><td> an array of seeds to initialize the random number generator.</td></tr><tr><td><span class="term"><em class="parameter"><code>seed_length</code></em> :</span></td><td> an array of seeds to initialize the random number generator.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td> the new <a href="glib-Random-Numbers.html#GRand"><span class="type">GRand</span></a>.</td></tr></tbody></table></div><p>Since 2.4</p></div><hr><div class="refsect2" lang="en"><a name="id3003531"></a><h3><a name="g-rand-new"></a>g_rand_new ()</h3><a class="indexterm" name="id3003542"></a><pre class="programlisting"><a href="glib-Random-Numbers.html#GRand">GRand</a>* g_rand_new (void);</pre><p>Creates a new random number generator initialized with a seed takeneither from <code class="filename">/dev/urandom</code> (if existing) or from the current time (as a fallback).</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td> the new <a href="glib-Random-Numbers.html#GRand"><span class="type">GRand</span></a>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3003594"></a><h3><a name="g-rand-copy"></a>g_rand_copy ()</h3><a class="indexterm" name="id3003605"></a><pre class="programlisting"><a href="glib-Random-Numbers.html#GRand">GRand</a>* g_rand_copy (<a href="glib-Random-Numbers.html#GRand">GRand</a> *rand_);</pre><p>Copies a <a href="glib-Random-Numbers.html#GRand"><span class="type">GRand</span></a> into a new one with the same exact state as before.This way you can take a snapshot of the random number generator forreplaying later.</p><p></p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>rand_</code></em> :</span></td><td> a <a href="glib-Random-Numbers.html#GRand"><span class="type">GRand</span></a>.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td> the new <a href="glib-Random-Numbers.html#GRand"><span class="type">GRand</span></a>.</td></tr></tbody></table></div><p>Since 2.4</p></div><hr><div class="refsect2" lang="en"><a name="id3003694"></a><h3><a name="g-rand-free"></a>g_rand_free ()</h3><a class="indexterm" name="id3003705"></a><pre class="programlisting">void g_rand_free (<a href="glib-Random-Numbers.html#GRand">GRand</a> *rand_);</pre><p>Frees the memory allocated for the <a href="glib-Random-Numbers.html#GRand"><span class="type">GRand</span></a>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>rand_</code></em> :</span></td><td> a <a href="glib-Random-Numbers.html#GRand"><span class="type">GRand</span></a>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3003768"></a><h3><a name="g-rand-set-seed"></a>g_rand_set_seed ()</h3><a class="indexterm" name="id3003779"></a><pre class="programlisting">void g_rand_set_seed (<a href="glib-Random-Numbers.html#GRand">GRand</a> *rand_, <a href="glib-Basic-Types.html#guint32">guint32</a> seed);</pre><p>Sets the seed for the random number generator <a href="glib-Random-Numbers.html#GRand"><span class="type">GRand</span></a> to <em class="parameter"><code>seed</code></em>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>rand_</code></em> :</span></td><td> a <a href="glib-Random-Numbers.html#GRand"><span class="type">GRand</span></a>.</td></tr><tr><td><span class="term"><em class="parameter"><code>seed</code></em> :</span></td><td> a value to reinitialize the random number generator.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3003870"></a><h3><a name="g-rand-set-seed-array"></a>g_rand_set_seed_array ()</h3><a class="indexterm" name="id3003881"></a><pre class="programlisting">void g_rand_set_seed_array (<a href="glib-Random-Numbers.html#GRand">GRand</a> *rand_, const <a href="glib-Basic-Types.html#guint32">guint32</a> *seed, <a href="glib-Basic-Types.html#guint">guint</a> seed_length);</pre><p>Initializes the random number generator by an array oflongs. Array can be of arbitrary size, though only thefirst 624 values are taken. This function is usefulif you have many low entropy seeds, or if you require more then32bits of actual entropy for your application.</p><p></p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>rand_</code></em> :</span></td><td> a <a href="glib-Random-Numbers.html#GRand"><span class="type">GRand</span></a>.</td></tr><tr><td><span class="term"><em class="parameter"><code>seed</code></em> :</span></td><td> array to initialize with</td></tr>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -