<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Time Intervals</title>
		<link>http://www.allegro.cc/forums/view/618888</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 03 Feb 2024 22:51:11 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>All,</p><p>Years ago I created a small function to return random numbers.  The calling function would send a limit and the return would send a number between 0 and the limit. I used a time function to set the seed. It&#39;s worked pretty good over the years until now.</p><p>I have a test program for randomly locating pixels to the screen.  The program writes random numbers to an array.  It then uses the array to  locate the pixels.   The issue is the random number to the array loops so fast that the results are uniformed.  I figure it&#39;s time to get away from the C time function and look for a newer one.  I briefly read something about nanoseconds with a C++ call.  Any recommendations?  Is there a more current way of creating random numbers?</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="p">#include&lt;iostream&gt;</span>
<span class="number">  2</span><span class="p">#include&lt;cstdio&gt;</span>
<span class="number">  3</span><span class="p">#include&lt;cstdlib&gt;</span>
<span class="number">  4</span><span class="p">#include&lt;time.h&gt;</span>
<span class="number">  5</span><span class="p">#include &lt;sys/time.h&gt;</span>
<span class="number">  6</span>
<span class="number">  7</span><span class="k1">using</span> <span class="k1">namespace</span> std<span class="k2">;</span>
<span class="number">  8</span>
<span class="number">  9</span><span class="k1">int</span> random_no<span class="k2">(</span><span class="k1">int</span> limit<span class="k2">)</span><span class="k2">{</span>
<span class="number"> 10</span>
<span class="number"> 11</span>     <span class="k1">char</span> ct<span class="k2">[</span><span class="n">28</span><span class="k2">]</span><span class="k2">;</span>
<span class="number"> 12</span>     <span class="k1">struct</span> timeval tv<span class="k2">;</span>
<span class="number"> 13</span>     <span class="k1">unsigned</span> <span class="k1">int</span> number,modnumber<span class="k2">;</span>
<span class="number"> 14</span>     <span class="k1">long</span> <span class="k1">int</span> seed<span class="k2">;</span>
<span class="number"> 15</span>     <span class="k1">static</span> <span class="k1">int</span> offset <span class="k3">=</span> <span class="n">0</span> <span class="k2">;</span>
<span class="number"> 16</span>     <span class="k1">char</span> run<span class="k2">;</span>
<span class="number"> 17</span>
<span class="number"> 18</span>    seed<span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_821.html" target="_blank">time</a><span class="k2">(</span>NULL<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 19</span>
<span class="number"> 20</span><span class="c">//  gettimeofday(&amp;tv, NULL);</span>
<span class="number"> 21</span><span class="c">//  seed = tv.tv_usec;</span>
<span class="number"> 22</span>    
<span class="number"> 23</span>    <a href="http://www.delorie.com/djgpp/doc/libc/libc_739.html" target="_blank">srand</a><span class="k2">(</span>seed<span class="k3">+</span>offset<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 24</span>
<span class="number"> 25</span>    modnumber <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_637.html" target="_blank">rand</a><span class="k2">(</span><span class="k2">)</span>%limit<span class="k2">;</span>
<span class="number"> 26</span>    offset<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
<span class="number"> 27</span>    <span class="k1">return</span><span class="k2">(</span>modnumber<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 28</span>
<span class="number"> 29</span><span class="k2">}</span> <span class="c">//end random_no()</span>
</div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (AceBlkwell)</author>
		<pubDate>Wed, 24 Jan 2024 23:29:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/618888/1054051#target">AceBlkwell</a> said:</div><div class="quote"><p> The issue is the random number to the array loops so fast that the results are uniformed.</p></div></div><p>You should call <span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_739.html" target="_blank">srand</a></span> just once, not every time you want a new random number. <span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_739.html" target="_blank">srand</a></span> resets the number sequence that <span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_637.html" target="_blank">rand</a></span> produces. So if you end up with the same seed, you get the same output.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Thu, 25 Jan 2024 07:14:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>What torhu said! You could use a static bool in your function so you can keep calling it when needed but it only seeds the first time.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Dizzy Egg)</author>
		<pubDate>Thu, 25 Jan 2024 15:31:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>AFAIC, <span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_739.html" target="_blank">srand</a></span> is something you call in <span class="source-code">main<span class="k2">(</span><span class="k2">)</span></span> once at the start, most often one of the first lines you call. It&#39;s in the same category as <span class="source-code"><a href="http://www.allegro.cc/manual/al_init"><span class="a">al_init</span></a><span class="k2">(</span><span class="k2">)</span></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (MiquelFire)</author>
		<pubDate>Thu, 25 Jan 2024 19:00:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks all, I&#39;ll give that a try.  I figured there had to be a better approach. </p><p>After the advice, I was thinking along the same lines as you Dizzy.  I want to keep the function self-contained.  Thanks again all.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (AceBlkwell)</author>
		<pubDate>Thu, 25 Jan 2024 22:27:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>srand and rand is sufficient for most purposes.</p><p>The mersenne twister PRNG (Psuedo random number generator) provides better more random results across ranges of values. And you seed and poll it just like rand.</p><p><img src="http://www.allegro.cc/forums/smileys/cool.gif" alt="8-)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sat, 27 Jan 2024 02:10:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If you&#39;re in a C++ mood you could try:
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#include &lt;random&gt;</span>

<span class="k1">int</span> random_no<span class="k2">(</span><span class="k1">int</span> limit<span class="k2">)</span> <span class="k2">{</span>
  <span class="k1">static</span> std::minstd_rand gen<span class="k2">;</span>
  <span class="k1">return</span> std::uniform_int_distribution<span class="k3">&lt;</span>int&gt;<span class="k2">(</span><span class="n">0</span>, limit-1<span class="k2">)</span><span class="k2">(</span>gen<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
Supposedly <span class="source-code">minstd_rand</span> is a better random number generator than <span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_637.html" target="_blank">rand</a><span class="k2">(</span><span class="k2">)</span></span>. There are a few internet articles saying don&#39;t use <span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_637.html" target="_blank">rand</a><span class="k2">(</span><span class="k2">)</span>%limit</span> and a few more saying it doesn&#39;t matter.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Hull)</author>
		<pubDate>Fri, 02 Feb 2024 02:55:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks Peter, I figured there had to be a more current command.</p><p>As of now though, I updated my code to include a bool that limits the seeding to one time.  It seems to work well.  I also caught that I have a couple variables declared that I don&#39;t use for anything.  I removed them (int number &amp; char run)  Must have used them years ago or was at least trialing a new approach back in the day.</p><p>Meanwhile, can you tell me if your approach would eliminate the need for different time functions, depending on while compiler I&#39;m using?  IE no need to seed?</p><p>Edgar, was this what you were mentioning as well?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (AceBlkwell)</author>
		<pubDate>Fri, 02 Feb 2024 03:57:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Mersenne twister is in c++11. Pretty simple use, just google it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 02 Feb 2024 21:33:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks Edgar.  I just read the Wiki.  Worth looking into deeper. I&#39;ll check it out.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (AceBlkwell)</author>
		<pubDate>Fri, 02 Feb 2024 22:54:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You can see how I wrapped it to get regular numbers in my Eagle library.</p><p><a href="https://github.com/EdgarReynaldo/EagleGUI/blob/master/src/Eagle/Random.cpp">https://github.com/EdgarReynaldo/EagleGUI/blob/master/src/Eagle/Random.cpp</a><br /><a href="https://github.com/EdgarReynaldo/EagleGUI/blob/master/include/Eagle/Random.hpp">https://github.com/EdgarReynaldo/EagleGUI/blob/master/include/Eagle/Random.hpp</a><br /><img src="http://www.allegro.cc/forums/smileys/cool.gif" alt="8-)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sat, 03 Feb 2024 22:51:11 +0000</pubDate>
	</item>
</rss>
