⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 glib-random-numbers.html

📁 最新gtk中文资料集
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"><title>Random Numbers</title><meta name="generator" content="DocBook XSL Stylesheets V1.73.2"><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.9 (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"><link rel="index" href="ix06.html" title="Index of new symbols in 2.8"><link rel="index" href="ix07.html" title="Index of new symbols in 2.10"><link rel="index" href="ix08.html" title="Index of new symbols in 2.12"><link rel="index" href="ix09.html" title="Index of new symbols in 2.14"><link rel="index" href="ix10.html" title="Index of new symbols in 2.16"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" id="top" 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><tr><td colspan="5" class="shortcuts"><nobr><a href="#id3050375" class="shortcut">Top</a>                  &#160;|&#160;                  <a href="#id3050836" class="shortcut">Description</a></nobr></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><a name="id3050375"></a><span class="refentrytitle">Random Numbers</span></h2><p>Random Numbers &#8212; 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 &lt;glib.h&gt;                    <a class="link" href="glib-Random-Numbers.html#GRand">GRand</a>;<a class="link" href="glib-Random-Numbers.html#GRand">GRand</a>*              <a class="link" href="glib-Random-Numbers.html#g-rand-new-with-seed">g_rand_new_with_seed</a>                (<a class="link" href="glib-Basic-Types.html#guint32">guint32</a> seed);<a class="link" href="glib-Random-Numbers.html#GRand">GRand</a>*              <a class="link" href="glib-Random-Numbers.html#g-rand-new-with-seed-array">g_rand_new_with_seed_array</a>          (const <a class="link" href="glib-Basic-Types.html#guint32">guint32</a> *seed,                                                         <a class="link" href="glib-Basic-Types.html#guint">guint</a> seed_length);<a class="link" href="glib-Random-Numbers.html#GRand">GRand</a>*              <a class="link" href="glib-Random-Numbers.html#g-rand-new">g_rand_new</a>                          (void);<a class="link" href="glib-Random-Numbers.html#GRand">GRand</a>*              <a class="link" href="glib-Random-Numbers.html#g-rand-copy">g_rand_copy</a>                         (<a class="link" href="glib-Random-Numbers.html#GRand">GRand</a> *rand_);void                <a class="link" href="glib-Random-Numbers.html#g-rand-free">g_rand_free</a>                         (<a class="link" href="glib-Random-Numbers.html#GRand">GRand</a> *rand_);void                <a class="link" href="glib-Random-Numbers.html#g-rand-set-seed">g_rand_set_seed</a>                     (<a class="link" href="glib-Random-Numbers.html#GRand">GRand</a> *rand_,                                                         <a class="link" href="glib-Basic-Types.html#guint32">guint32</a> seed);void                <a class="link" href="glib-Random-Numbers.html#g-rand-set-seed-array">g_rand_set_seed_array</a>               (<a class="link" href="glib-Random-Numbers.html#GRand">GRand</a> *rand_,                                                         const <a class="link" href="glib-Basic-Types.html#guint32">guint32</a> *seed,                                                         <a class="link" href="glib-Basic-Types.html#guint">guint</a> seed_length);#define             <a class="link" href="glib-Random-Numbers.html#g-rand-boolean">g_rand_boolean</a>                      (rand_)<a class="link" href="glib-Basic-Types.html#guint32">guint32</a>             <a class="link" href="glib-Random-Numbers.html#g-rand-int">g_rand_int</a>                          (<a class="link" href="glib-Random-Numbers.html#GRand">GRand</a> *rand_);<a class="link" href="glib-Basic-Types.html#gint32">gint32</a>              <a class="link" href="glib-Random-Numbers.html#g-rand-int-range">g_rand_int_range</a>                    (<a class="link" href="glib-Random-Numbers.html#GRand">GRand</a> *rand_,                                                         <a class="link" href="glib-Basic-Types.html#gint32">gint32</a> begin,                                                         <a class="link" href="glib-Basic-Types.html#gint32">gint32</a> end);<a class="link" href="glib-Basic-Types.html#gdouble">gdouble</a>             <a class="link" href="glib-Random-Numbers.html#g-rand-double">g_rand_double</a>                       (<a class="link" href="glib-Random-Numbers.html#GRand">GRand</a> *rand_);<a class="link" href="glib-Basic-Types.html#gdouble">gdouble</a>             <a class="link" href="glib-Random-Numbers.html#g-rand-double-range">g_rand_double_range</a>                 (<a class="link" href="glib-Random-Numbers.html#GRand">GRand</a> *rand_,                                                         <a class="link" href="glib-Basic-Types.html#gdouble">gdouble</a> begin,                                                         <a class="link" href="glib-Basic-Types.html#gdouble">gdouble</a> end);void                <a class="link" href="glib-Random-Numbers.html#g-random-set-seed">g_random_set_seed</a>                   (<a class="link" href="glib-Basic-Types.html#guint32">guint32</a> seed);#define             <a class="link" href="glib-Random-Numbers.html#g-random-boolean">g_random_boolean</a>                    ()<a class="link" href="glib-Basic-Types.html#guint32">guint32</a>             <a class="link" href="glib-Random-Numbers.html#g-random-int">g_random_int</a>                        (void);<a class="link" href="glib-Basic-Types.html#gint32">gint32</a>              <a class="link" href="glib-Random-Numbers.html#g-random-int-range">g_random_int_range</a>                  (<a class="link" href="glib-Basic-Types.html#gint32">gint32</a> begin,                                                         <a class="link" href="glib-Basic-Types.html#gint32">gint32</a> end);<a class="link" href="glib-Basic-Types.html#gdouble">gdouble</a>             <a class="link" href="glib-Random-Numbers.html#g-random-double">g_random_double</a>                     (void);<a class="link" href="glib-Basic-Types.html#gdouble">gdouble</a>             <a class="link" href="glib-Random-Numbers.html#g-random-double-range">g_random_double_range</a>               (<a class="link" href="glib-Basic-Types.html#gdouble">gdouble</a> begin,                                                         <a class="link" href="glib-Basic-Types.html#gdouble">gdouble</a> end);</pre></div><div class="refsect1" lang="en"><a name="id3050836"></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 class="ulink" 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 class="link" 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 class="link" 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 class="link" 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 class="link" 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="id3050987"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id3050997"></a><h3><a name="GRand"></a>GRand</h3><a class="indexterm" name="id3051010"></a><pre class="programlisting">typedef struct _GRand GRand;</pre><p>The <a class="link" 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="id3051040"></a><h3><a name="g-rand-new-with-seed"></a>g_rand_new_with_seed ()</h3><a class="indexterm" name="id3051054"></a><pre class="programlisting"><a class="link" href="glib-Random-Numbers.html#GRand">GRand</a>*              g_rand_new_with_seed                (<a class="link" 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><p><span class="term"><em class="parameter"><code>seed</code></em>&#160;:</span></p></td><td> a value to initialize the random number generator.</td></tr><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span>&#160;:</span></p></td><td> the new <a class="link" 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="id3051127"></a><h3><a name="g-rand-new-with-seed-array"></a>g_rand_new_with_seed_array ()</h3><a class="indexterm" name="id3051143"></a><pre class="programlisting"><a class="link" href="glib-Random-Numbers.html#GRand">GRand</a>*              g_rand_new_with_seed_array          (const <a class="link" href="glib-Basic-Types.html#guint32">guint32</a> *seed,                                                         <a class="link" 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><p><span class="term"><em class="parameter"><code>seed</code></em>&#160;:</span></p></td><td> an array of seeds to initialize the random number generator.</td></tr><tr><td><p><span class="term"><em class="parameter"><code>seed_length</code></em>&#160;:</span></p></td><td> an array of seeds to initialize the random number generator.</td></tr><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span>&#160;:</span></p></td><td> the new <a class="link" href="glib-Random-Numbers.html#GRand"><span class="type">GRand</span></a>.</td></tr></tbody></table></div><p class="since">Since  2.4</p></div><hr><div class="refsect2" lang="en"><a name="id3051247"></a><h3><a name="g-rand-new"></a>g_rand_new ()</h3>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -