<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.2.3" -->
<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/"
	>

<channel>
	<title>PHP-Scripts Blog &#187; Random</title>
	<link>http://www.php-scripts.com</link>
	<description>Writing about PHP scripting since 12/99. Learn something new every day.</description>
	<pubDate>Thu, 09 Oct 2008 14:17:07 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.3</generator>
	<language>en</language>
			<item>
		<title>How to randomize, return and remove numbers from a pool</title>
		<link>http://www.php-scripts.com/20061211/97/</link>
		<comments>http://www.php-scripts.com/20061211/97/#comments</comments>
		<pubDate>Mon, 11 Dec 2006 17:30:17 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[Random]]></category>

		<category><![CDATA[How To]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/20061211/97/</guid>
		<description><![CDATA[This morning in the IRC chat (irc.scriptschool.com #scriptschool) a random visitor named Rnd-Amarion stopped by with the following how-to PHP question:
I am about to learn PHP, and want to program a simple web page with a long string of random numbers. I have the random numbers allready. When someone queries the web site, it retrieves [...]]]></description>
			<content:encoded><![CDATA[<p>This morning in the IRC chat (irc.scriptschool.com #scriptschool) a random visitor named Rnd-Amarion stopped by with the following how-to PHP question:<br />
<blockquote>I am about to learn PHP, and want to program a simple web page with a long string of random numbers. I have the random numbers allready. When someone queries the web site, it retrieves four of those numbers from the front of this list and gives them to the querier and then removes those numbers form the front fo the list.</p></blockquote>
<p>I asked him what were the range of numbers he wanted randomized. While this information wasn&#8217;t critical to writing the code to solve this how-to, it would better demonstrate to him (her? I didn&#8217;t ask). Didn&#8217;t get a response but I decided to help this unknown person anyway.</p>
<p>Let&#8217;s consult the PHP manual for the function array_rand (PHP 4, 5) and the definition:<br />
<blockquote>array_rand &#8212; Pick one or more random entries out of an array</p></blockquote>
<p>That sounds like the right function, but with most programming languages there are many ways to approach the same problem. Let&#8217;s create some code to do this using the PHP functions range, shuffle and array_shift.</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$random_numbers</span> = <a href="http://www.php.net/range"><span class="kw3">range</span></a><span class="br0">&#40;</span><span class="nu0">1</span>,<span class="nu0">100</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/shuffle"><span class="kw3">shuffle</span></a><span class="br0">&#40;</span><span class="re0">$random_numbers</span><span class="br0">&#41;</span>; <span class="co1">// mix up the order</span><br />
<span class="co1">// return the first four numbers when queried and remove from array</span><br />
<span class="re0">$numbers_chosen</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="re0">$random_numbers</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, <span class="re0">$random_numbers</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>, <span class="re0">$random_numbers</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>, <span class="re0">$random_numbers</span><span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;</p>
<p><a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;Before: &lt;br /&gt;&#8221;</span>;<br />
<a href="http://www.php.net/print_r"><span class="kw3">print_r</span></a><span class="br0">&#40;</span><span class="re0">$random_numbers</span><span class="br0">&#41;</span>; <span class="co1">// show all random numbers</span></p>
<p><span class="co1">// remove first four numbers</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="re0">$i</span>=<span class="nu0">0</span>;<span class="re0">$i</span>&amp;lt;<span class="nu0">4</span>;<span class="re0">$i</span>++<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<a href="http://www.php.net/array_shift"><span class="kw3">array_shift</span></a><span class="br0">&#40;</span><span class="re0">$random_numbers</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><a href="http://www.php.net/print"><span class="kw3">print</span></a><span class="st0">&#8220;&lt;hr /&gt;After: &#8220;</span>;<br />
<a href="http://www.php.net/print_r"><span class="kw3">print_r</span></a><span class="br0">&#40;</span><span class="re0">$random_numbers</span><span class="br0">&#41;</span>; <span class="co1">// show all remaining random numbers</span><br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a><span class="st0">&#8220;&lt;hr /&gt;Chosen: &#8220;</span>;<br />
<a href="http://www.php.net/print_r"><span class="kw3">print_r</span></a><span class="br0">&#40;</span><span class="re0">$numbers_chosen</span><span class="br0">&#41;</span>; <span class="co1">// first four numbers chosen </span><br />
&nbsp;</div>
</div>
<p>Example: <a href="http://www.php-scripts.com/examples/array_shift.php">code execution</a>, <a href="http://www.php-scripts.com/examples/array_shift.phps">code source</a></p>
<p>Notes: The array_shift function removes the first item in an array. The converse array_pop removes the last item in an array. Another useful array function is array_splice.</p>
<p><b>Update 9:55am PST</b>: You can see a more efficient version using <a href="http://www.php-scripts.com/examples/array_splice.phps">array_splice here</a> that eliminates the use of the for loop in the code above and the use of array_shift. As for which is more efficient for removing only one item in the array, that&#8217;s a test for you to run :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20061211/97/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Random web page background colors</title>
		<link>http://www.php-scripts.com/20060714/89/</link>
		<comments>http://www.php-scripts.com/20060714/89/#comments</comments>
		<pubDate>Fri, 14 Jul 2006 13:51:01 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[Random]]></category>

		<category><![CDATA[one liners]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/20060714/89/</guid>
		<description><![CDATA[If you want to create a random background color

&#60;?php $hex = dechex&#40;rand&#40;0,255&#41;&#41; . dechex&#40;rand&#40;0,255&#41;&#41; . dechex&#40;rand&#40;0,255&#41;&#41;; ?&#62;

and then you&#8217;d use the following in your body tag:

&#60;body BGCOLOR=&#8220;#&#60;?php echo($hex);?&#62;&#8221;&#62;

Related
How to change background color only on holidays
]]></description>
			<content:encoded><![CDATA[<p>If you want to create a random background color</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php <span class="re0">$hex</span> = <a href="http://www.php.net/dechex"><span class="kw3">dechex</span></a><span class="br0">&#40;</span><a href="http://www.php.net/rand"><span class="kw3">rand</span></a><span class="br0">&#40;</span><span class="nu0">0</span>,<span class="nu0">255</span><span class="br0">&#41;</span><span class="br0">&#41;</span> . <a href="http://www.php.net/dechex"><span class="kw3">dechex</span></a><span class="br0">&#40;</span><a href="http://www.php.net/rand"><span class="kw3">rand</span></a><span class="br0">&#40;</span><span class="nu0">0</span>,<span class="nu0">255</span><span class="br0">&#41;</span><span class="br0">&#41;</span> . <a href="http://www.php.net/dechex"><span class="kw3">dechex</span></a><span class="br0">&#40;</span><a href="http://www.php.net/rand"><span class="kw3">rand</span></a><span class="br0">&#40;</span><span class="nu0">0</span>,<span class="nu0">255</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; <span class="kw2">?&gt;</span></div>
</div>
<p>and then you&#8217;d use the following in your body tag:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;body BGCOLOR=<span class="st0">&#8220;#&lt;?php echo($hex);?&gt;&#8221;</span>&gt;</div>
</div>
<p><b>Related</b><br />
<a href="http://www.php-scripts.com/20051018/45/">How to change background color only on holidays</a></body></p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20060714/89/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to not show the same thing twice when randomizing</title>
		<link>http://www.php-scripts.com/20051212/72/</link>
		<comments>http://www.php-scripts.com/20051212/72/#comments</comments>
		<pubDate>Tue, 13 Dec 2005 04:04:20 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[Random]]></category>

		<category><![CDATA[How To]]></category>

		<category><![CDATA[Sessions]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/?p=72</guid>
		<description><![CDATA[Last night I was reviewing a new site that randomizes pages and I kept seeing the same pages coming up repeatedly. This is a rather common problem with many basic randomizers that do not keep track of what has already been shown. 

This begs the question: how do you setup even distribution of a randomized [...]]]></description>
			<content:encoded><![CDATA[<p>Last night I was reviewing a new site that randomizes pages and I kept seeing the same pages coming up repeatedly. This is a rather common problem with many basic randomizers that do not keep track of what has already been shown. </p>
<p><img src="http://www.php-scripts.com/images/2005/evendistribution.jpg" border="0" ALT="How to evenly distribute a pool of randomized content screenshot"/></p>
<p>This begs the question: how do you setup even distribution of a randomized pool of content? There are a couple different ways to do this, some of which are extremely efficient, but today we&#8217;ll review one that is very efficient and scales nicely.</p>
<p>First we need to pre-randomize the results and save an index of where we are at in the randomized pool. For example let&#8217;s say you have 10 numbers you want to randomize like this:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="re0">$numbers</span> = <a href="http://www.php.net/range"><span class="kw3">range</span></a><span class="br0">&#40;</span><span class="nu0">1</span>,<span class="nu0">10</span><span class="br0">&#41;</span>; <span class="co1">// fill with numbers 1-10</span><br />
<a href="http://www.php.net/shuffle"><span class="kw3">shuffle</span></a><span class="br0">&#40;</span><span class="re0">$numbers</span><span class="br0">&#41;</span>; <span class="co1">// as of PHP 4.2.0 no need to seed with srand()</span><br />
<span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re0">$numbers</span> <span class="kw1">as</span> <span class="re0">$each</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;#1: $each&lt;br /&gt;&#8221;</span>; <span class="co1">// display all random numbers </span><br />
<span class="br0">&#125;</span><br />
<span class="kw2">?&gt;</span></div>
</div>
<p>Now you would just need to remember the position of the randomized array from page to page so that the next randomized item in the array is displayed.  To do this within the same browsing session we coud use sessions. To do this across browser sessions, we could use cookies. Here is some code illustrating how to do this using a session:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<a href="http://www.php.net/session_start"><span class="kw3">session_start</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$_SESSION</span><span class="br0">&#91;</span><span class="st0">&#8216;index&#8217;</span><span class="br0">&#93;</span> == <span class="nu0">10</span> or <span class="re0">$_SESSION</span><span class="br0">&#91;</span><span class="st0">&#8216;index&#8217;</span><span class="br0">&#93;</span> == <span class="st0">&#8221;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// first or LAST time, generate numbers</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$_SESSION</span><span class="br0">&#91;</span><span class="st0">&#8216;index&#8217;</span><span class="br0">&#93;</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$rnumbers</span> = <a href="http://www.php.net/range"><span class="kw3">range</span></a><span class="br0">&#40;</span><span class="nu0">1</span>,<span class="nu0">10</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/shuffle"><span class="kw3">shuffle</span></a><span class="br0">&#40;</span><span class="re0">$rnumbers</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$_SESSION</span><span class="br0">&#91;</span><span class="st0">&#8216;numbers&#8217;</span><span class="br0">&#93;</span> = <span class="re0">$rnumbers</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="re0">$curindex</span> = <span class="re0">$_SESSION</span><span class="br0">&#91;</span><span class="st0">&#8216;index&#8217;</span><span class="br0">&#93;</span>;<br />
<span class="re0">$rnumbers</span> = <span class="re0">$_SESSION</span><span class="br0">&#91;</span><span class="st0">&#8216;numbers&#8217;</span><span class="br0">&#93;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;Current randomize (#&#8217;</span> . <span class="br0">&#40;</span><span class="re0">$curindex</span>+<span class="nu0">1</span><span class="br0">&#41;</span> . <span class="st0">&#8216;): &lt;b&gt;&#8217;</span> .&nbsp; <span class="re0">$rnumbers</span><span class="br0">&#91;</span><span class="re0">$curindex</span><span class="br0">&#93;</span>&nbsp; . <span class="st0">&#8216;&lt;hr /&gt;&#8217;</span>;<br />
<span class="re0">$x</span> = <span class="nu0">1</span>;<br />
<span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re0">$rnumbers</span> <span class="kw1">as</span> <span class="re0">$each</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="br0">&#40;</span><span class="re0">$x</span>-<span class="nu0">1</span><span class="br0">&#41;</span> == <span class="re0">$curindex</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;&lt;font color=&#8217;green&#8217;&gt;&lt;b&gt;#$x: $each&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;&#8221;</span>; <span class="co1">// display all random numbers</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;#$x: $each&lt;br /&gt;&#8221;</span>; <span class="co1">// display all random numbers</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$x</span>++;<br />
<span class="br0">&#125;</span><br />
<span class="re0">$_SESSION</span><span class="br0">&#91;</span><span class="st0">&#8216;index&#8217;</span><span class="br0">&#93;</span> = <span class="re0">$curindex</span>+<span class="nu0">1</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;&lt;hr /&gt;Refresh &lt;a href=&quot;even_distribution.php&quot;&gt;or click&lt;/a&gt; for next randomized number. When all 10 numbers have cycled, a new shuffle of numbers in the pool will occur.&#8217;</span>;<br />
<span class="kw2">?&gt;</span></div>
</div>
<p><b>Notes:</b> start the code with session_start() to initialize the session. Then we check to see if the $_SESSION[&#8217;index&#8217;] is set or has reached the ceiling of the array. If the ceiling has been reached (10 numbers) then we reshuffle the array and reset the index to the first position.</p>
<p>This code also illustrates how to highlight the current item selected in a foreach loop in a bold green font. Since arrays begin numbering at zero but lists start numbering at 1, it is necessary to add and subtract one where applicable in the code. The $x variable is a list counter variable.</p>
<p><b>What if you want to add new content to the array?</b><br />
Say you are currently at item #6 and there has been two new numbers added? You could wait until the current cycle is complete and re-randomize the numbers for that user or you could re-randomize the remaining indices with the new content added pushed to the end. You could then fill a new array with the remaining numbers, shuffle that and then <i>replace</i> the existing $numbers array starting at the next index number. This way the user would not see the content they&#8217;d already seen and would see the new content randomized into the list.</p>
<p><b>Above and beyond</b><br />
This technique could be used for anything you could put into an array. If the array size is very big then you might want to work in stages, for example only load in 50 or 100 items into the session variables at a time as opposed to loading in thousands of possible pointers. Once the user has reached the random point, just grab a new randomized set of numbers from set #2 then set #3, etc all the way until all the pool has been exhausted.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20051212/72/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
