Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » as » Generate Random Number Using Random.org

Generate Random Number Using Random.org

How to create a truly random number using the random.org API.

I'm sure you are all familiar with the previous tutorials on random numbers and probably noticed some limitations in letting the computer choose a random number. A number generated on the computer is actually "psuedo-random" which in most cases is adequate. However in some applications you need a truly random number, such as a drawing, contest or lottery based app.

Well with the help of a 3rd party API, random.org in this example, we can quickly get a truly random number. Random.org generates these random numbers based on atmospheric noise, which is in most cases much more random than a traditional generator in a computer.

Start by creating a new Flash or Flex document and open the actions/code panel. Then add the required imports and URL to the API.

import flash.net.URLLoader;
import flash.events.Event;

var RANDOM_DOTORG_URL:String = 
	"http://www.random.org/integers/?num=1&min=0&max=%s&col=1&base=10&format=plain&rnd=new";

Once that is completed set up the sample array, which in this case is a basic list of fruits. This list could be loaded from a database or other service.

var fruits:Array = [
	'Apples', 'Apricots', 'Bananas', 'Blueberries', 'Cantaloupe', 'Cranberries', 'Figs', 
	'Grapefruit', 'Grapes', 'Kiwifruit', 'Lemon/Limes', 'Oranges', 'Papaya', 'Pears', 
	'Pineapple', 'Plums', 'Prunes', 'Raisins', 'Raspberries', 'Strawberries', 'Watermelon'
];

The API URL needs to contain the length of the recently created array so for simplicity we'll replace the %s with the length of the array.

RANDOM_DOTORG_URL = RANDOM_DOTORG_URL.split("%s").join(fruits.length-1);

Next create the url loader which makes the call to the API service.

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, dataLoaded);
loader.load(new URLRequest(RANDOM_DOTORG_URL));

Finally create the event handler that in our sample application will trace out the random fruit. In a full application you could process this result and display a different image or video, for example.

function dataLoaded(evt:Event):void
{
	var id:int = int(evt.currentTarget.data);
	trace("All Fruits: " + fruits);
	trace("Random Fruit: " + fruits[id]);
}

As you can see this script is pretty straightforward. Feel free to use/edit this code for you own projects and as always, happy coding!

Here is the complete script, enjoy.

/*
	Scriptplayground
	http://v2.scriptplayground.com
	
	THIS SOFTWARE IS PROVIDED BY SCRIPTPLAYGROUND "AS IS" AND ANY EXPRESS 
	OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
	WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL SCRIPTPLAYGROUND OR CONTRIBUTORS BE 
	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
	BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
	WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
	OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
	EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

// Required imports
import flash.net.URLLoader;
import flash.events.Event;

// The url to random.org
var RANDOM_DOTORG_URL:String = 
	"http://www.random.org/integers/?num=1&min=0&max=%s&col=1&base=10&format=plain&rnd=new";

// our sample array
var fruits:Array = [
	'Apples', 'Apricots', 'Bananas', 'Blueberries', 'Cantaloupe', 'Cranberries', 'Figs', 
	'Grapefruit', 'Grapes', 'Kiwifruit', 'Lemon/Limes', 'Oranges', 'Papaya', 'Pears', 
	'Pineapple', 'Plums', 'Prunes', 'Raisins', 'Raspberries', 'Strawberries', 'Watermelon'
];

// replace "%s" with the length of the fruits array
RANDOM_DOTORG_URL = RANDOM_DOTORG_URL.split("%s").join(fruits.length-1);

// create new URLLoader instance to load data
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, dataLoaded);
loader.load(new URLRequest(RANDOM_DOTORG_URL));

// event handler to process response, in this case the random Id
function dataLoaded(evt:Event):void
{
	var id:int = int(evt.currentTarget.data);
	trace("All Fruits: " + fruits);
	trace("Random Fruit: " + fruits[id]);
}

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Generate Random Number Using Random.org

 realizzazione siti palerm  Wed Nov 2, 2011 11:57 pm  
thanks for this script is useful to fill a database,
regards by RSP
Add a comment
The ability to add new comments has been disabled in this archived version.

Loading
Main | Tutorials | Articles