<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>sodium | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/sodium/feed/" rel="self" type="application/rss+xml" />
	<link>https://learncgames.com</link>
	<description>A blog about C, programming games and my ebook(s).</description>
	<lastBuildDate>Fri, 24 Apr 2020 19:14:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://learncgames.com/wp-content/uploads/2020/03/cropped-favicon-32x32.png</url>
	<title>sodium | Learn C Games Programming Blog</title>
	<link>https://learncgames.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">181446779</site>	<item>
		<title>More on generating random numbers</title>
		<link>https://learncgames.com/more-on-generating-random-numbers/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=more-on-generating-random-numbers</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Sat, 02 May 2020 23:00:42 +0000</pubDate>
				<category><![CDATA[Source code]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[sodium]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=508</guid>

					<description><![CDATA[<p>The very first program I wrote in any programming language was sometime in October 1976. It was in BASIC and it had this line or something very similar. Even then I was into random numbers. I know, uppercase! LET DIEROLL = RANDOM(6)+1 I&#8217;ve been writing small utility that I&#8217;ll publish in a few days. It&#8217;s [&#8230;]</p>
The post <a href="https://learncgames.com/more-on-generating-random-numbers/">More on generating random numbers</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:755,&quot;href&quot;:&quot;https:\/\/pixabay.com\/users\/Clker-Free-Vector-Images-3736\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=25637&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260115073407\/https:\/\/pixabay.com\/users\/clker-free-vector-images-3736\/?utm_source=link-attribution&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 18:16:27&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-13 01:37:55&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-17 12:07:36&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-28 07:43:10&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-13 05:25:37&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-04 23:26:15&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-19 17:46:05&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-19 17:46:05&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:756,&quot;href&quot;:&quot;https:\/\/pixabay.com\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=25637&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260205102120\/https:\/\/pixabay.com\/?utm_source=link-attribution&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 18:16:28&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-13 01:37:55&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-17 12:07:36&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-28 07:43:10&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-13 05:25:38&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-04 23:26:17&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-19 17:46:02&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-19 17:46:02&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:757,&quot;href&quot;:&quot;https:\/\/doc.libsodium.org&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260131173815\/https:\/\/doc.libsodium.org\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 18:16:32&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-13 01:37:57&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-17 12:07:37&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-28 07:43:11&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-13 05:25:38&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-04 23:26:21&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-04-19 17:46:12&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-19 17:46:12&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]'></div>
<figure id="attachment_510" aria-describedby="caption-attachment-510" style="width: 300px" class="wp-caption alignleft"><img fetchpriority="high" decoding="async" class="size-medium wp-image-510" src="https://learncgames.com/wp-content/uploads/2020/04/dice-25637_640-300x187.png" alt="Two dice" width="300" height="187" srcset="https://learncgames.com/wp-content/uploads/2020/04/dice-25637_640-300x187.png 300w, https://learncgames.com/wp-content/uploads/2020/04/dice-25637_640.png 640w" sizes="(max-width: 300px) 100vw, 300px" /><figcaption id="caption-attachment-510" class="wp-caption-text">Image by <a href="https://pixabay.com/users/Clker-Free-Vector-Images-3736/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=25637">Clker-Free-Vector-Images</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=25637">Pixabay</a></figcaption></figure>
<p>The very first program I wrote in any programming language was sometime in October 1976. It was in BASIC and it had this line or something very similar. Even then I was into random numbers. I know, uppercase!</p>
<pre><code class="language-visual-basic" data-line="">LET DIEROLL = RANDOM(6)+1</code></pre>
<p>I&#8217;ve been writing small utility that I&#8217;ll publish in a few days. It&#8217;s 100% in C and I decided instead of using <em>srand(time(null))</em> for generating random numbers, I&#8217;d use something with a bit more oomph. That something is a library called <a href="https://doc.libsodium.org/" target="_blank" rel="noopener noreferrer">sodium</a>. Using it for just generating random numbers is a bit overkill. But it has the benefit that the random numbers are good enough random to be used in cryptographic code. Not all random number generators are good enough. This one is.</p>
<p>And it&#8217;s easy enough and adds just one dll, or you can compile it statically. I&#8217;ll do that once I figure it exactly which Visual Studio settings to change.</p>
<p>It takes just one include &#8211;</p>
<pre><code class="language-c" data-line="">#include &lt;sodium.h&gt;</code></pre>
<p>Then one call to initialise it.</p>
<pre><code class="language-c" data-line=""> int i = sodium_init();
 if (i&lt;0 ) {
   error(&quot;Unable to initialise sodium library. Error = &quot;,inttoa(i) );
 }</code></pre>
<p>After that you can call one of several functions. Here&#8217;s an example from my code. It fills a 64 byte key with the numbers 0-63 but shuffled like cards.</p>
<pre><code class="language-c" data-line="">
    typedef char key[64];
    key _key;
    uint32_t ival;
    int i,index1,index2;
    for (i = 0; i &lt; 63; i++) {
        _key[i] = i;
    }
    for (i = 0; i &lt; 1000; i++) {
        do {
            index1 = randombytes_uniform(64);
            index2 = randombytes_uniform(64);
        } while (index1 == index2);
        ival = _key[index2]; // swap two indices
        _key[index2] = _key[index1];
        _key[index1] = ival;
    }</code></pre>
<p>The do loop (<em>how often do you actually see those used in code!</em>) generates two indexes in the range 0-63, making sure that they are not the same. It then swaps the two values at those indexes, and does this 1,000 times. It&#8217;s very similar to card deck shuffling code.</p>
<p>Do you know how long it would take you to work through all the combinations of 64 numbers this way? IF you could do a million a second, it would take you 1.27&#215;10<sup>83</sup> seconds or 4.02&#215;10<sup>75</sup> years! Or to make it more meaningful it&#8217;s this large! 4.02,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000. That&#8217;s approaching (but still far far off) the time for the heat death of the universe which is estimated at 10<sup>100</sup>! </p>The post <a href="https://learncgames.com/more-on-generating-random-numbers/">More on generating random numbers</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">508</post-id>	</item>
	</channel>
</rss>
