Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a way to generate true random numbers in Flash Builder?
    primarykey
    data
    text
    <p>I have a project that generates strings of random numbers that are used to select items from a test bank. I noticed that some of the items were being selected at disproportionately high rates so I decided to check the 'randomness' of Math.Random. The following code produces randomly ordered lists of the numbers 0 through n-1. It then counts the number of times the first item is a 0, 1, 2,... ,n-1 You can change the number of samples that are generated with the horizontal slider. What I have produced appears to indicate that the random numbers are not at all random For example, if I select 100 samples of a six digit string, I get the following distribution suggesting that 0, and 5 are greatly under represented: 11,23,15,18,24,9 This pattern holds as I re-run the simulation. I've checked my code but would greatly appreciate the insight of others concerning the accuracy of this. I've heard that AS3 does not produce true random numbers, but can they really be this bad?</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600" &gt; &lt;mx:Script&gt; &lt;![CDATA[ import mx.events.ListEvent; private var startingArray:Array = []; private var questionsArray:Array; private var countArray:Array; private var randomNumbers:int = 3; private function calculate():void{ countArray = [0,0,0,0,0,0,0,0,0,0]; for( var i:int = 0; i &lt; slider.value; i++){ questionsArray = [];//Reset the list of questions createRandomListOfQuestions(randomNumbers); } result.text = String(countArray); } public function createRandomListOfQuestions(_numQuestions:int):void{ //Create an array containing the sequence of test questions var numQuestions:int=_numQuestions; //Reset the array startingArray=[];//Contains a randomized question order for (var i:int=0;i&lt;numQuestions; i++){//Create an array of question numbers starting at 1 var count:int = 0 startingArray.push(i); } //splice() removes one or more elements from an array and returns the deleted elements, //here only one (as specified in the second argument) while (startingArray.length &gt; 0) {//Create a randomized list (questionsArray) from startingArray var val:int =startingArray.splice(Math.round(Math.random() * (startingArray.length - 1)), 1) questionsArray.push(val); if(count == 0){ countArray[val] += 1 count++ } } questionsArrayText.text += String(questionsArray) + "\r"; } private function changeEvt(event:Event):void { randomNumbers = event.currentTarget.selectedItem.data } ]]&gt; &lt;/mx:Script&gt; &lt;mx:VBox horizontalCenter="0" verticalCenter="0"&gt; &lt;mx:Text x="487" y="261" text="{}" width="500" id="result"/&gt; &lt;mx:ComboBox change="changeEvt(event)" &gt; &lt;mx:ArrayCollection&gt; &lt;mx:Object label="Three" data="3"/&gt; &lt;mx:Object label="Four" data="4"/&gt; &lt;mx:Object label="Five" data="5"/&gt; &lt;mx:Object label="Six" data="6"/&gt; &lt;mx:Object label="Seven" data="7"/&gt; &lt;mx:Object label="Eight" data="8"/&gt; &lt;mx:Object label="Nine" data="9"/&gt; &lt;mx:Object label="Ten" data="10"/&gt; &lt;/mx:ArrayCollection&gt; &lt;/mx:ComboBox&gt; &lt;mx:Button label="New list" click="calculate()"/&gt; &lt;mx:HSlider id="slider" value="5" minimum="5" maximum="100" snapInterval="1" /&gt; &lt;mx:Label text="Random Numbers: {Math.round(slider.value) }"/&gt; &lt;/mx:VBox&gt; &lt;mx:Text id="questionsArrayText" horizontalCenter="0" verticalCenter="0"/&gt; &lt;/mx:Application&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload