<?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>MaxoTech Blog &#187; Visual Basic</title>
	<atom:link href="http://maxotek.net/blog/category/programming/visual-basic/feed" rel="self" type="application/rss+xml" />
	<link>http://maxotek.net/blog</link>
	<description>Technology Blog</description>
	<lastBuildDate>Mon, 19 Jul 2010 07:38:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Non Repeating &#8211; Random Number Generation Class</title>
		<link>http://maxotek.net/blog/non-repeating-random-number-generation-class-t9.html</link>
		<comments>http://maxotek.net/blog/non-repeating-random-number-generation-class-t9.html#comments</comments>
		<pubDate>Thu, 08 Feb 2007 08:29:20 +0000</pubDate>
		<dc:creator>partho</dc:creator>
				<category><![CDATA[Visual Basic]]></category>

		<guid isPermaLink="false">http://maxotek.net/blog/?p=9</guid>
		<description><![CDATA[Sometimes, we require to generate non-repeating random numbers. Here&#8217;s the code I wrote to do just that. The numbers to be generated can be within the specified range or passed as a param array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Public Class RandomNumber
    Dim NumDomain As New ArrayList      ' The Numbers to be generated
 [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, we require to generate non-repeating random numbers. Here&#8217;s the code I wrote to do just that. The numbers to be generated can be within the specified range or passed as a param array.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
</pre></td><td class="code"><pre class="vbnet"><span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Class</span> RandomNumber
    <span style="color: #0600FF;">Dim</span> NumDomain <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> ArrayList      <span style="color: #008080; font-style: italic;">' The Numbers to be generated</span>
    <span style="color: #0600FF;">Dim</span> Nums <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> ArrayList           <span style="color: #008080; font-style: italic;">' The Numbers waiting to be generated</span>
&nbsp;
    <span style="color: #0600FF;">Sub</span> <span style="color: #FF8000;">New</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> Start <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>, <span style="color: #FF8000;">ByVal</span> Finish <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span><span style="color: #000000;">&#41;</span>
        <span style="color: #008080; font-style: italic;">' Sequential Numbers</span>
        <span style="color: #0600FF;">Dim</span> I <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>
        <span style="color: #FF8000;">For</span> I <span style="color: #008000;">=</span> Start <span style="color: #FF8000;">To</span> Finish
            NumDomain.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>I<span style="color: #000000;">&#41;</span>
        <span style="color: #FF8000;">Next</span>
&nbsp;
        AddItems<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
&nbsp;
    <span style="color: #0600FF;">Sub</span> <span style="color: #FF8000;">New</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> <span style="color: #FF8000;">ParamArray</span> Numbers<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span><span style="color: #000000;">&#41;</span>
        <span style="color: #008080; font-style: italic;">' Numbers in a Param Array</span>
        <span style="color: #0600FF;">Dim</span> Number <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>
        <span style="color: #FF8000;">For</span> <span style="color: #0600FF;">Each</span> Number In Numbers
            NumDomain.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>Number<span style="color: #000000;">&#41;</span>
        <span style="color: #FF8000;">Next</span>
&nbsp;
        AddItems<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
&nbsp;
    <span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> AddItems<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
        Nums.<span style="color: #0000FF;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #008080; font-style: italic;">' Insert All Numbers Into the Array</span>
        <span style="color: #0600FF;">Dim</span> I <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>
        <span style="color: #FF8000;">For</span> I <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">To</span> NumDomain.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span>
            Nums.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>NumDomain<span style="color: #000000;">&#40;</span>I<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #FF8000;">Next</span>
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
&nbsp;
    <span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Function</span> GetRandomNumber<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>
        <span style="color: #0600FF;">If</span> Nums.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span>
            <span style="color: #008080; font-style: italic;">' Re-add the Items, when all the numbers have been generated.</span>
            AddItems<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">' Return a Random Item and Remove it from the List</span>
        <span style="color: #0600FF;">Dim</span> Ch <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">' Using Timer as the seed for the Random Number Generation</span>
        <span style="color: #0600FF;">Randomize</span><span style="color: #000000;">&#40;</span>Microsoft.<span style="color: #0000FF;">VisualBasic</span>.<span style="color: #0600FF;">Timer</span><span style="color: #000000;">&#41;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">' Random Number is generated within the range.</span>
        Ch <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">+</span> <span style="color: #0600FF;">CInt</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">Rnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">*</span> <span style="color: #000000;">&#40;</span>Nums.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
&nbsp;
        <span style="color: #0600FF;">Dim</span> Num <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> <span style="color: #008000;">=</span> Nums<span style="color: #000000;">&#40;</span>Ch<span style="color: #000000;">&#41;</span>
        Nums.<span style="color: #0000FF;">RemoveAt</span><span style="color: #000000;">&#40;</span>Ch<span style="color: #000000;">&#41;</span>
        <span style="color: #FF8000;">Return</span> Num
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Class</span></pre></td></tr></table></div>

<h5>Mechanism</h5>
<p>The overloaded constructors allow numbers to be generated either within a range or from the numbers passed as parameters. Internally there are two array lists: <b>NumDomain</b> &#038; <b>Num</b>. The former contains all the numbers within the domain and the latter holds the numbers that have not been generated. The <b>GetRandomNumber</b> function returns a random number from the <b>Num</b> array list and also removes it from there. When all the numbers have been poped (not to be confused with stack poping) out, the <b>Num</b> array list is re-filled with the values in <b>NumDomain</b> and the cycle continues. Thus, allowing no repeats per cycle.</p>
]]></content:encoded>
			<wfw:commentRss>http://maxotek.net/blog/non-repeating-random-number-generation-class-t9.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
