<?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>GuShH&#039;s DevBlog &#187; random</title>
	<atom:link href="http://gushh.net/blog/?tag=random&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://gushh.net/blog</link>
	<description>This blog is about software, electronics engineering and game development.</description>
	<lastBuildDate>Tue, 24 Jan 2012 00:31:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>MSP430 Launchpad: Random Software Delays</title>
		<link>http://gushh.net/blog/random-software-delays/</link>
		<comments>http://gushh.net/blog/random-software-delays/#comments</comments>
		<pubDate>Mon, 13 Dec 2010 07:04:49 +0000</pubDate>
		<dc:creator>GuShH</dc:creator>
				<category><![CDATA[MSP430 Launchpad]]></category>
		<category><![CDATA[DIY]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Launchpad]]></category>
		<category><![CDATA[Maker]]></category>
		<category><![CDATA[MSP430]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[Texas Instruments]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://gushh.net/blog/?p=659</guid>
		<description><![CDATA[The goal: to &#8220;randomly&#8221; flash the on-board LEDs at P1.0 and P1.6 The weapons: CCS, Launchpad, Cookies (you may choose your favourite ones) The reason: To familiarize yourself with the coding environment, or just for the heck of it. The library: #ifndef GRAND_H_ #define GRAND_H_ &#160; static unsigned long int rand_next = 1; &#160; int gRand&#40; [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The goal:</strong> to &#8220;randomly&#8221; flash the on-board LEDs at P1.0 and P1.6</p>
<p><strong>The weapons:</strong> CCS, Launchpad, Cookies (you may choose your favourite ones)</p>
<p><strong>The reason</strong>: To familiarize yourself with the coding environment, or just for the heck of it.</p>
<h3>The library:</h3>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#ifndef GRAND_H_</span>
<span style="color: #339900;">#define GRAND_H_</span>
&nbsp;
<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> rand_next <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> gRand<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">void</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	rand_next <span style="color: #000080;">=</span> rand_next <span style="color: #000040;">*</span> <span style="color: #0000dd;">1103515245</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">12345</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>rand_next<span style="color: #000040;">/</span><span style="color: #0000dd;">65536</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span> <span style="color: #0000dd;">32768</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> gSRand<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> seed <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	rand_next <span style="color: #000080;">=</span> seed<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #339900;">#define gRandom()		((gRand() &amp;amp; 0x7fff) / ((float)0x7fff)) 	// random in the range [0, 1]</span>
<span style="color: #339900;">#define gCRandom()		(2.0 * (gRandom() - 0.5)) 			// random in the range [-1, 1]</span>
<span style="color: #339900;">#define gMax(x,y)		(x &amp;gt; y ? x : y)				// minimum</span>
<span style="color: #339900;">#define gMin(x,y)		(x &amp;lt; y ? x : y)				// maximum</span>
&nbsp;
<span style="color: #339900;">#endif /*GRAND_H_*/</span></pre></div></div>

<h3>That&#8217;s our random library, it&#8217;s grand.h</h3>
<p><em>The &#8220;g&#8221; prefix is one I often use privately, it&#8217;s simply the first letter of my name. However because there may be other routines in the future with a similar naming convention, having &#8220;g&#8221; prefixed is not a bad idea; without having to fall into namespace gibberish.</em></p>
<p>The PRNG is an old standard. No need to discuss it.</p>
<p>You may recognize those macros, yes! They&#8217;re from Quake3! &#8212; Although we aren&#8217;t using them I left them there for future reference on how to obtain usable value ranges from the PRNG. There&#8217;s a lot to be said about floating point values and whatnot, But I&#8217;m going to restrain myself in this case.</p>
<h3>Now to the main code:</h3>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// MSP430 Launchpad - Blink onboard LEDs using random delays.</span>
<span style="color: #666666;">//	GuShH - info@gushh.net</span>
&nbsp;
<span style="color: #339900;">#include  &quot;msp430x20x2.h&quot;	// Include the necessary header for our target MCU</span>
<span style="color: #339900;">#include  &quot;grand.h&quot;		// Include our simplistic prng lib.</span>
&nbsp;
<span style="color: #0000ff;">void</span> delay_ms<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> ms <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// This function simply performs a software delay, hard from efficient but it's practical in this case.</span>
	<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> i<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #000080;">=</span> ms<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// Make sure your &lt; and = has no space in between (the syntax parser seems to be messing things up)</span>
		__delay_cycles<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">500</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// This should be dependent on clock speed... but what the hell, even the loop itself should be taken into account...</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> set_and_wait<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> led <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	P1OUT <span style="color: #000080;">=</span> led<span style="color: #008080;">;</span>			<span style="color: #666666;">// Set the bits</span>
	delay_ms<span style="color: #008000;">&#40;</span> gRand<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> <span style="color:#800080;">0.01</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	<span style="color: #666666;">// Delay a &quot;random&quot; amount of time</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
	WDTCTL <span style="color: #000080;">=</span> WDTPW <span style="color: #000040;">+</span> WDTHOLD<span style="color: #008080;">;</span> <span style="color: #666666;">// Hold the WDT (WatchDog Timer)</span>
	P1DIR <span style="color: #000040;">|</span><span style="color: #000080;">=</span> BIT0<span style="color: #000040;">|</span>BIT6<span style="color: #008080;">;</span>       <span style="color: #666666;">// Enable the appropriate output</span>
&nbsp;
	<span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		set_and_wait<span style="color: #008000;">&#40;</span> BIT0 <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	<span style="color: #666666;">// Set BIT0, that's our first LED.</span>
		set_and_wait<span style="color: #008000;">&#40;</span> BIT6 <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	<span style="color: #666666;">// Set BIT6, The other LED.</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>No external hardware is required, just make sure both P1.0 and P1.6 jumpers are set.</p>
<p>As you can see we&#8217;re simply toggling the LEDs with a random delay, the delay_ms(); function was taken from <a href="http://processors.wiki.ti.com/index.php/Playing_The_Imperial_March">here</a>.</p>
<p>Like I said there are quite a few topics to explain, however I decided to keep this one as simple as possible (Alright, I&#8217;m in a rush!)</p>
<p>So&#8230; Compile, run and enjoy!</p>
<p>Once I get the time I&#8217;ll put together some utilitarian code libraries and lengthier explanations, promise.</p>
<p>For those interested, you may download the entire project directory from here: <a href='http://gushh.net/blog/wp-content/uploads/2010/12/Random-Software-Delays.zip'>Random Software Delays</a>.</pre>
<g:plusone href='http://gushh.net/blog/random-software-delays/'></g:plusone>]]></content:encoded>
			<wfw:commentRss>http://gushh.net/blog/random-software-delays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Structuring libraries, basic design tips.</title>
		<link>http://gushh.net/blog/structuring-libraries-basic-design-tips/</link>
		<comments>http://gushh.net/blog/structuring-libraries-basic-design-tips/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 23:20:56 +0000</pubDate>
		<dc:creator>GuShH</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[PureBasic]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[instancing]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[prng]]></category>
		<category><![CDATA[programming tutorial]]></category>
		<category><![CDATA[purebasic]]></category>
		<category><![CDATA[random]]></category>

		<guid isPermaLink="false">http://gushh.net/blog/?p=227</guid>
		<description><![CDATA[One of the most important things a programmer has to know and learn is proper code design, structuring of the code, etc. This may not be imperative for you if you&#8217;re just starting out, but sooner or later you&#8217;ll be able to spot the patterns and realize that your current way of doing things is [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most important things a programmer has to know and learn is proper code design, structuring of the code, etc. This may not be imperative for you if you&#8217;re just starting out, but sooner or later you&#8217;ll be able to spot the patterns and realize that your current way of doing things is not optimal but rather cumbersome. This is when you start to separate things into modules and you begin to design your code with more meaning and purpose.</p>
<p>By breaking your code into modules or &#8220;classes&#8221; you can have a finer granularity of the code itself, you can encapsulate the base code and work on top of it, you can also reuse the code in other projects if you have a good design. An example would be a log library (for logging useful debugging information onto text files) with proper design and care you&#8217;ll be able to use the same library in almost all of your projects with little to no modification at all.<br />
<span id="more-227"></span><br />
The main points you should never forget about are:</p>
<ul>
<li>Instancing
<ul>
<li>This is when you create an &#8220;object&#8221; &#8211; And as you create it, you should also be able to destroy it. (Constructors / Destructors in OOP). An object factory may be implemented (this would be another class) to handle the instances. <em>In CPP you may use smart pointers, but we&#8217;re talking about PB in this case.</em></li>
</ul>
</li>
<li>Members
<ul>
<li>In our case a main structure is used as the &#8220;members&#8221; (Again, in reference to OOP) &#8211; This structure is instantiated by the constructor or &#8220;create&#8221; function. Don&#8217;t forget that anything you allocate you should also be able to deallocate. It doesn&#8217;t matter if the language or OS handles it underneath; it&#8217;s bad coding practice to avoid such trivial &#8211; but important &#8211; code.</li>
</ul>
</li>
<li>Methods
<ul>
<li>They give you functionality, in PB there are no methods but a similar concept applies: you have functions or routines that you create specifically for the library itself. Some may be of the Get / Set type (which either get or set a variable) Or other methods such as converters, helpers, accesibility calls, etc.</li>
</ul>
</li>
</ul>
<p>All together they make a class; in our case a library.</p>
<p>Let&#8217;s write one and follow by example!</p>
<p>Imagine you needed to encapsulate the random functions to provide extra functionality, then all you need to do is first define a structure (if you&#8217;re going to have members; for instance do you want the library to &#8220;remember&#8221; the previous and current numbers?, if so you would need a structure!)</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">;- Library Structure</span>
<span style="color: #000066; font-weight: bold;">Structure</span> <span style="color: #0000ff;">RANDOM</span>
	LastNumber.i
	PreviousNumber.i
	Seed.i
<span style="color: #000066; font-weight: bold;">EndStructure</span></pre></div></div>

<p>Alright, now we need to write our constructor and destructor functions! This will create and destroy the instances.</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">;- Library Instancing</span>
<span style="color: #000066; font-weight: bold;">Procedure</span>.i RandomCreate<span style="color: #000066;">&#40;</span> Seed.i <span style="color: #000066;">=</span> <span style="color: #CC0000;">0</span> <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Define</span>.<span style="color: #0000ff;">RANDOM</span> <span style="color: #000066;">*</span>this <span style="color: #000066;">=</span> <span style="color: #0000ff;">AllocateMemory</span><span style="color: #000066;">&#40;</span> <span style="color: #0000ff;">SizeOf</span><span style="color: #000066;">&#40;</span> <span style="color: #0000ff;">RANDOM</span> <span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066; font-weight: bold;">Not</span> Seed
			Seed <span style="color: #000066;">=</span> <span style="color: #0000ff;">ElapsedMilliseconds</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">With</span> <span style="color: #000066;">*</span>this
			 <span style="color: #000066;">\</span>LastNumber 		<span style="color: #000066;">=</span> <span style="color: #CC0000;">0</span>
			 <span style="color: #000066;">\</span>PreviousNumber 	<span style="color: #000066;">=</span> <span style="color: #CC0000;">0</span>
			 <span style="color: #000066;">\</span>Seed 			<span style="color: #000066;">=</span> Seed
		<span style="color: #000066; font-weight: bold;">EndWith</span>
&nbsp;
		<span style="color: #0000ff;">RandomSeed</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>Seed <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this
&nbsp;
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i RandomDestroy<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.<span style="color: #0000ff;">RANDOM</span> <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<span style="color: #0000ff;">FreeMemory</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this <span style="color: #000066;">&#41;</span>
		<span style="color: #000066;">*</span>this <span style="color: #000066;">=</span> #Null
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this
&nbsp;
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span></pre></div></div>

<p>That&#8217;s not too bad, first we allocate an instance and then we populate the member variables just to return the pointer of our new &#8220;object&#8221;. The destructor simply frees any allocated memory and clears out the pointer itself.</p>
<p>Now we need to define our library functions, in this case we only need one so we may generate a random number and keep track of the previous and current numbers.</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">;- Library functions</span>
<span style="color: #000066; font-weight: bold;">Procedure</span>.i RandomGetInteger<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.<span style="color: #0000ff;">RANDOM</span>, min.i, max.i <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>PreviousNumber 	<span style="color: #000066;">=</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>LastNumber
		<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>LastNumber 	<span style="color: #000066;">=</span> <span style="color: #000066;">&#40;</span> min <span style="color: #000066;">+</span> <span style="color: #0000ff;">Random</span><span style="color: #000066;">&#40;</span> max<span style="color: #000066;">-</span>min <span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>LastNumber
&nbsp;
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span></pre></div></div>

<p>That&#8217;s ok, but we can still add functionality. For instance, we need a way to access the structure fields (or &#8220;members&#8221;) without having to cast pointers, etc. We can do this by implementing a few Get/Set routines. In this case we only need to get the previous and current numbers, so&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">;- Helper functions</span>
<span style="color: #000066; font-weight: bold;">Procedure</span>.i RandomGetLast<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.<span style="color: #0000ff;">RANDOM</span> <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>LastNumber
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i RandomGetPrevious<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.<span style="color: #0000ff;">RANDOM</span> <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>PreviousNumber
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span></pre></div></div>

<p>But wait, we can still add functionality! such as utility functions, in this case we would like to fill a given array with random numbers, so&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">;- Utility functions</span>
<span style="color: #000066; font-weight: bold;">Procedure</span>.i RandomFillArray<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.<span style="color: #0000ff;">RANDOM</span>, min.i, max.i, Array DestArray.i<span style="color: #000066;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<span style="color: #000066; font-weight: bold;">Define</span>.i Size, i
		Size <span style="color: #000066;">=</span> ArraySize<span style="color: #000066;">&#40;</span> DestArray<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>, <span style="color: #CC0000;">1</span> <span style="color: #000066;">&#41;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">If</span> Size
			<span style="color: #000066; font-weight: bold;">For</span> i<span style="color: #000066;">=</span><span style="color: #CC0000;">0</span> <span style="color: #000066; font-weight: bold;">To</span> Size
				DestArray<span style="color: #000066;">&#40;</span>i<span style="color: #000066;">&#41;</span> <span style="color: #000066;">=</span> RandomGetInteger<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this, min, max <span style="color: #000066;">&#41;</span>
			<span style="color: #000066; font-weight: bold;">Next</span>
		<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span></pre></div></div>

<p>And while we&#8217;re at it, let&#8217;s fill lists as well!</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">Procedure</span>.i RandomFillList<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.<span style="color: #0000ff;">RANDOM</span>, min.i, max.i, elements.i, List DestList.i<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>, Clear.i<span style="color: #000066;">=</span>#False <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<span style="color: #000066; font-weight: bold;">If</span> Clear
			<span style="color: #0000ff;">ClearList</span><span style="color: #000066;">&#40;</span>DestList<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">Define</span>.i i
&nbsp;
		<span style="color: #000066; font-weight: bold;">If</span> elements
			<span style="color: #000066; font-weight: bold;">For</span> i<span style="color: #000066;">=</span><span style="color: #CC0000;">1</span> <span style="color: #000066; font-weight: bold;">To</span> elements
				<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #0000ff;">AddElement</span><span style="color: #000066;">&#40;</span> DestList<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
					DestList<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">=</span> RandomGetInteger<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this, min, max <span style="color: #000066;">&#41;</span>
				<span style="color: #000066; font-weight: bold;">EndIf</span>
			<span style="color: #000066; font-weight: bold;">Next</span>
		<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span></pre></div></div>

<p>If you don&#8217;t need to keep track of the current and previous numbers, you can just opt out the instancing system and modify the utility calls to work in an &#8220;instance-less&#8221; mode, however that&#8217;s up to you.</p>
<p>But, let&#8217;s take it for a ride first!</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">;- Example</span>
<span style="color: #000066; font-weight: bold;">Define</span>.<span style="color: #0000ff;">RANDOM</span> <span style="color: #000066;">*</span>rnd <span style="color: #000066;">=</span> RandomCreate<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">Define</span>.i i
&nbsp;
<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>rnd
&nbsp;
	<span style="color: #000066; font-weight: bold;">For</span> i<span style="color: #000066;">=</span><span style="color: #CC0000;">0</span> <span style="color: #000066; font-weight: bold;">To</span> <span style="color: #CC0000;">99</span>
		<span style="color: #000066; font-weight: bold;">Debug</span> RandomGetInteger<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>rnd, <span style="color: #CC0000;">5</span>, <span style="color: #CC0000;">10</span> <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">Next</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Debug</span> <span style="color: #009900;">&quot;--&quot;</span>
	<span style="color: #000066; font-weight: bold;">Debug</span> RandomGetLast<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>rnd <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">Debug</span> RandomGetPrevious<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>rnd <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">Debug</span> <span style="color: #009900;">&quot;--&quot;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Dim</span> TestArray.i<span style="color: #000066;">&#40;</span><span style="color: #CC0000;">9</span><span style="color: #000066;">&#41;</span>
	RandomFillArray<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>rnd, <span style="color: #CC0000;">100</span>, <span style="color: #CC0000;">200</span>, TestArray<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">For</span> i<span style="color: #000066;">=</span><span style="color: #CC0000;">0</span> <span style="color: #000066; font-weight: bold;">To</span> <span style="color: #CC0000;">9</span>
		<span style="color: #000066; font-weight: bold;">Debug</span> TestArray<span style="color: #000066;">&#40;</span>i<span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">Next</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Debug</span> <span style="color: #009900;">&quot;--&quot;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">NewList</span> TestList.i<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
	RandomFillList<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>rnd, <span style="color: #CC0000;">10</span>, <span style="color: #CC0000;">20</span>, <span style="color: #CC0000;">5</span>, TestList<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">ForEach</span> TestList<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">Debug</span> TestList<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">Next</span>
&nbsp;
	RandomDestroy<span style="color: #000066;">&#40;</span><span style="color: #000066;">*</span>rnd<span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">EndIf</span></pre></div></div>

<p>Ok, so we tested the instancing and most if not all of the &#8220;methods&#8221; we just created. By now you should realize how useful such systems can be if you happen to design them correctly.</p>
<p>This particular lib could be extended to support averaging of the numbers, median / mean, etc. You could even implement a custom PRNG and give the option to choose between yours and the built-in one!.</p>
<p>You may download the entire source from <a href="http://gushh.net/dev/?file=pb/random.pb">here</a>. As an exercise you could implement the Get/Set methods for the Seed.</p>
<p>Cheers!</p>
<p>PS: Someone remind me to update the language file for GeShi and to fix the font <img src='http://gushh.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  (The indentation is all wrong!)</p>
<g:plusone href='http://gushh.net/blog/structuring-libraries-basic-design-tips/'></g:plusone>]]></content:encoded>
			<wfw:commentRss>http://gushh.net/blog/structuring-libraries-basic-design-tips/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Randomize Array &#8211; A Flexible hard-coded macro</title>
		<link>http://gushh.net/blog/randomize-array-a-flexible-hard-coded-macro/</link>
		<comments>http://gushh.net/blog/randomize-array-a-flexible-hard-coded-macro/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 16:13:40 +0000</pubDate>
		<dc:creator>GuShH</dc:creator>
				<category><![CDATA[PureBasic]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[fill]]></category>
		<category><![CDATA[format]]></category>
		<category><![CDATA[macro]]></category>
		<category><![CDATA[pb]]></category>
		<category><![CDATA[purebasic]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[randomize]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://gushh.net/blog/2009/06/13/pb-randomize-array-a-flexible-hard-coded-macro/</guid>
		<description><![CDATA[Tired of writing the same &#8220;randomize array&#8221; routines over and over?, just snap this little macro and register them as you see fit. A guard should be put to avoid double registrations (although the compiler will complain about this anyway &#8212; I suggest registering right after you include this, since you already know what you&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<p>Tired of writing the same &#8220;randomize array&#8221; routines over and over?, just snap this little macro and register them as you see fit. A guard should be put to avoid double registrations (although the compiler will complain about this anyway &#8212; I suggest registering right after you include this, since you already know what you&#8217;re going to need&#8230; )</p>
<p>Code:<br />
<a href="http://gushh.net/dev/?file=pb/randomizearray.pb" target="_blank"> http://gushh.net/dev/?file=pb/randomizearray.pb</a></p>
<p>Examples:<br />
<a href="http://gushh.net/dev/?file=pb/randomizearray_test.pb" target="_blank"> http://gushh.net/dev/?file=pb/randomizearray_test.pb</a></p>
<p>Simple, right?</p>
<p>A need for both a debug / format (to string) and random fills for string arrays is present, but I&#8217;m not in the need of them at the moment. Do request if you need them though.</p>
<p>Cheers.</p>
<g:plusone href='http://gushh.net/blog/randomize-array-a-flexible-hard-coded-macro/'></g:plusone>]]></content:encoded>
			<wfw:commentRss>http://gushh.net/blog/randomize-array-a-flexible-hard-coded-macro/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Served from: gushh.net @ 2012-02-09 07:11:50 by W3 Total Cache -->
