Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 09-21-2007, 01:40 AM   #1
Registered User
Join Date: Sep 2007
Posts: 2
Default Generating lots of random numbers

Hey, I'm using flash Professional 8.

my problem is needing to generate a lot of random numbers in flash at once. the numbers typically end up being the same or very close to the same number because the random numbers are fed by how long the movie has been running.

I was thinking maybe I could somehow "seed" the random numbers to avoid this. ie have an array of numbers that i hard code in. ie... 1,2.5,6,4,2,8,7,0 which would help "seed" the random number because each time a random number is generated it won't be the same number over and over because the seed is significantly different enough to create numbers that are different enough.

Is doing something like this possible in any way?

What I really need is a way to generate a lot of numbers(within a range, ie between 5 and 10) (this is all done milliseconds within the last number generated) in succession without the numbers being the same number over and over.

Whats the best way to go about doing this?
misterdan is offline   Reply With Quote
Old 09-21-2007, 02:27 AM   #2
throw a trace() in there
Join Date: Dec 2006
Posts: 1,982

I think this is not as much of a problem as you think it is.

Pseudorandom number generators seeded with 1 and 1.1 are not any more likely to have the same results than generators seeded with 1 and 1000.
rrh is offline   Reply With Quote
Old 09-21-2007, 02:33 AM   #3
Super Moderator
xxneon's Avatar
Join Date: Dec 2006
Location: Amish Country, PA
Posts: 8,570
Send a message via ICQ to xxneon Send a message via AIM to xxneon Send a message via MSN to xxneon Send a message via Skype™ to xxneon

how many random numbers do you need generated at one single time..
Always optimizing...
xxneon is offline   Reply With Quote
Old 09-21-2007, 04:06 AM   #4
Registered User
Join Date: Sep 2007
Posts: 2

In a nutsheel, I have a randomly moving graphic that picks random pixel "destinations" on the screen then moves to them at a specified speed. The problem I am having is picking the random locations.

Right now my system works except that the X and Y coordinates of the destination seem to always turn out to be the same (the X is about the same value as the Y). This being probably because one is generated right after the other (it seems because of the clock based seed that this is what the problem is).

The random number generating function looks like this...

function randomRange(min:Number, max:Number):Number 

		var theresult;

		theresult = Math.round(Math.random() * (max - min + 1)) + min;
			return min;
		}else if(theresult>max){
			return max;
			return theresult;
running this function twice in a run usually returns the same "random" number (or very close to the same number)
btw to answer your question about how many random numbers are generated. i would say each new destination is calculated about every 2-3 seconds, and calculating a new destination involves generating a random speed,X coordinate, and Y coordinate. so every time new numbers need to be generated there is AT LEAST 3 (but sometimes several numbers are being generated for each destination because it is possible for certain random numbers to be "invalid" in which a new random number needs to be chosen)

Hopefully I didn't confuse anyone >.<

Last edited by misterdan; 09-21-2007 at 04:09 AM.
misterdan is offline   Reply With Quote
Old 09-21-2007, 05:28 AM   #5
throw a trace() in there
Join Date: Dec 2006
Posts: 1,982

I defined your function as above, then ran this code as a test:
ActionScript Code:
list = new Array(); for (var count=0;count<5000;count++) {     list.push(randomRange(1, 100)); } trace(list); recur = [0,0,0,0,0,0,0,0,0,0,0,0]; var prev=-1; var occur=0; for (var count=0;count<5000;count++) {     if (list[count]==prev) {         occur++;         recur[occur-1]--;         recur[occur]++;     }     else     {         occur=0;     }     prev=list[count]; } trace(recur);
This generates a random list of 5000 numbers between 1 and 100. I usually get close to 50 pairs, and sometimes one or two triples. This is what you would expect statistically. So I am not encountering the problem you are trying to solve.

One thing with your function is that the max and min are half as likely as any of the numbers in the middle, since you are using round() instead of floor().

If there is anything seriously skewing your numbers, it is probably the criteria you use to determine which ones are "invalid."
rrh is offline   Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
random not that random? CDHBookingEdge ActionScript 3.0 9 08-31-2010 04:30 PM
generating random numbers with exceptions asprinwizard ActionScript 2.0 0 10-04-2006 10:52 AM
Generating random numbers i_am_parag ActionScript 1.0 (and below) 2 08-07-2006 11:06 AM
generating random numbers connman ActionScript 1.0 (and below) 23 11-29-2005 06:38 PM
Generating Unique Random Numbers spooky_po ActionScript 1.0 (and below) 2 04-17-2003 06:45 AM

All times are GMT. The time now is 05:32 AM.

Follow actionscriptorg on Twitter


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.