<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="6" MadCap:lastHeight="94" MadCap:lastWidth="853" MadCap:disableMasterStylesheet="true" MadCap:tocPath="" MadCap:InPreviewMode="false" MadCap:PreloadImages="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" lang="en-us" xml:lang="en-us" MadCap:PathToHelpSystem="../../../" MadCap:HelpSystemFileName="Default.xml" MadCap:SearchType="Stem"> <head><title>RANDOM</title> <link href="../../SkinSupport/MadCap.css" rel="stylesheet" /> <link href="../../Resources/Stylesheets/BSE_Help.css" rel="stylesheet" /> <script src="../../SkinSupport/MadCapAll.js" type="text/javascript"> </script> </head> <body> <h1 class="code">RANDOM</h1> <div class="ImagePlusCaption"> <div class="Col2"> <p> <img src="../../graphics/pgm_icon1.gif" border="0" alt="BS1 icon" title="BS1 icon" /> <img src="../../graphics/pgm_icon2.gif" border="0" alt="BS2 icon" title="BS2 icon" /> <img src="../../graphics/pgm_icon2e.gif" border="0" alt="BS2e icon" title="BS2e icon" /> <img src="../../graphics/pgm_icon2sx.gif" border="0" alt="BS2sx icon" title="BS2sx icon" /> <img src="../../graphics/pgm_icon2p.gif" border="0" alt="BS2p icon" title="BS2p icon" /> <img src="../../graphics/pgm_icon2pe.gif" border="0" alt="BS2pe icon" title="BS2pe icon" /> <img src="../../graphics/pgm_icon2px.gif" border="0" alt="BS2px icon" title="BS2px icon" /> </p> </div> <p style="text-align: right;"><a href="../ExampleTopics/RandomEx.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">RANDOM Examples</a> </p> <p> </p> </div> <p class="clear"> </p> <p> </p> <p class="PlainText">Syntax: <span class="keyword_in_text">RANDOM</span> <![CDATA[ ]]><i>Variable</i></p> <h2>Function</h2> <p class="PlainText">Generate a pseudo-random number. </p> <ul> <li value="1"><b><i>Variable</i></b> is a variable (usually a word) whose bits will be scrambled to produce a random number. <i>Variable</i> acts as <span class="keyword_in_text">RANDOM</span>'s input and its result output. Each pass through <span class="keyword_in_text">RANDOM</span> stores the next number, in the pseudo-random sequence, in <i>Variable</i>.</li> </ul> <h2>Explanation</h2> <p class="PlainText"><span class="keyword_in_text">RANDOM</span> generates pseudo-random numbers ranging from 0 to 65535. They're called "pseudo-random" because they appear random, but are generated by a logic operation that uses the initial value in <i>Variable</i> to "tap" into a sequence of 65535 essentially random numbers. If the same initial value, called the "seed", is always used, then the same sequence of numbers is generated. The following example demonstrates this:</p> <p> <img src="../../graphics/bs1_inline.gif" border="0"> </img> </p><pre class="BScode" xml:space="preserve"> SYMBOL result = W1 Main: result = 11000 ' set initial "seed" value RANDOM result ' generate random number DEBUG result ' show the result on screen GOTO Main </pre> <p> <img src="../../graphics/bs2all_inline.gif" border="0"> </img> </p><pre class="BScode" xml:space="preserve"> result VAR Word Main: result = 11000 ' set initial "seed" value RANDOM result ' generate random number DEBUG DEC ? result ' show the result on screen GOTO Main </pre> <p class="PlainText">In this example, the same number would appear on the screen over and over again. This is because the same seed value was used each time; specifically, the first line of the loop sets result to 11000. The <span class="keyword_in_text">RANDOM</span> command really needs a different seed value each time. Moving the "Result =" line out of the loop will solve this problem, as in:</p> <p> <img src="../../graphics/bs1_inline.gif" border="0"> </img> </p><pre class="BScode" xml:space="preserve"> SYMBOL result = W1 Setup: result = 11000 ' set initial "seed" value Main: RANDOM result ' generate random number DEBUG result ' show the result on screen GOTO Main END </pre> <p> <img src="../../graphics/bs2all_inline.gif" border="0"> </img> </p><pre class="BScode" xml:space="preserve"> result VAR Word Setup: result = 11000 ' set initial "seed" value Main: RANDOM result ' generate random number DEBUG DEC ? result ' show the result on screen GOTO Main END </pre> <p class="PlainText">Here, result is only initialized once, before the loop. Each time through the loop, the previous value of result, generated by <span class="keyword_in_text">RANDOM</span>, is used as the next seed value. This generates a more desirable set of pseudo-random numbers.</p> <p class="PlainText">In applications requiring more apparent randomness, it's necessary to "seed" <span class="keyword_in_text">RANDOM</span> with a more random value every time. For instance, in the example program, <span class="keyword_in_text">RANDOM</span> is executed continuously (using the previous resulting number as the next seed value) while the program waits for the user to press a button. Since the user can't control the timing of button presses very accurately, the results approach true randomness. </p> <div class="Col2"> <div class="MasterFoot"> <p MadCap:conditions="BSEconditions.BSEWebHelp (Primary)-INCLUDE"><a href="../../HomeTopics/HomePage.htm">Go to Welcome page</a> </p> </div> </div> <div class="Col2"> <div class="MasterFoot"> <p style="text-align: right;"><span class="ContactInfoProjectName">BASIC Stamp Help</span> <![CDATA[ ]]><span class="ContactInfoVersion#">Version 2.5.4</span> <![CDATA[ ]]></p> <p style="text-align: right;">Copyright © <span class="ContactInfoCompanyName">Parallax Inc.</span></p> <p style="text-align: right;"><span class="SystemShortDate">8/8/2012</span> </p> </div> </div> <script type="text/javascript">/* <![CDATA[ */ var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-285614-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); /* ]]> */</script> <script type="text/javascript" src="../../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>