Repeating a random number sequence...

  • stakemaster
  • Born
  • Born
  • stakemaster
  • Posts: 4

Post 3+ Months Ago

Hi, to make testing a little easier I need to be able to repeat a random number sequence in ActionScript (without saving and restoring the numbers) as you can in Visual Basic using the RND and RANDOMIZE statements? I'm using the RANDOM function, even though it's deprecated.

Many thanks in advance.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

random() is indeed deprecated, but it is still supported. And rightfully so, random() is faster than Math.random(). When it comes to more math intensive coding is when you really notice a difference, but I usually like to stick with random() anyway.

But now i'm confused you say you are using the random functino to achieve what you want (which is what you need to do), does that mean you have it figured out and was just wondering of an alternate way without using the deprecated random()?


If so, I say just stick with random(), but if you don't want to then go for Math.random(). It works a bit differently than random().

http://www.kirupa.com/developer/actions ... random.htm
  • stakemaster
  • Born
  • Born
  • stakemaster
  • Posts: 4

Post 3+ Months Ago

...I am using random() because it's convenient...I haven't yet figured it out...
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

Ok, well I don't use VB so i'm not entirely sure what you are looking for.

By sequence, do you mean random numbers one after another, or a long string of random numbers?
  • stakemaster
  • Born
  • Born
  • stakemaster
  • Posts: 4

Post 3+ Months Ago

...if I have a loop that calls the random() function 10 times, for example, with a parameter of 20, it will return a sequence of 10 numbers in the range 0 to 19, e.g. 3,11,15,10,4,2,17,18,9,5....what I want to do is repeat the loop and get the same sequence of numbers instead of 10 new ones.


fyi in VB...

Randomize Statement

Quote:
Initializes the random-number generator.

Syntax

Randomize [number]

The optional number argument is a Variant or any valid numeric expression.

Remarks

Randomize uses number to initialize the Rnd function's random-number generator, giving it a new seed value. If you omit number, the value returned by the system timer is used as the new seed value.

If Randomize is not used, the Rnd function (with no arguments) uses the same number as a seed the first time it is called, and thereafter uses the last generated number as a seed value.

Note

To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument.



Rnd function

Quote:
Returns a Single containing a random number.

Syntax

Rnd[(number)]

The optional number argument is a Single or any valid numeric expression.

Return Values

If number is less than zero, Rnd generates the same number every time, using number as the seed.
If number is greater than zero, Rnd generates the next random number in the sequence.
If number is equal to zero, Rnd generates the most recently generated number.
If number is not supplied, Rnd generates the next random number in the sequence.

Remarks

The Rnd function returns a value less than 1 but greater than or equal to zero.

The value of number determines how Rnd generates a random number:

For any given initial seed, the same number sequence is generated because each successive call to the Rnd function uses the previous number as a seed for the next number in the sequence.

To produce random integers in a given range, use this formula:

Int(([upperbound] - [lowerbound] + 1) * Rnd + [lowerbound])

Here, upperbound is a field with the highest number in the range, and lowerbound is a field with the lowest number in the range.

  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

Oh, in Flash you can't do that unless you store the values somehow, best choice probably being an array.

Simple example (it's 2:30am, im still packing and getting ready to move, so this is VERY quick)
Code: [ Select ]
var myArray = [];
for (var i = 0; i<10; i++) {
    myArray[i] = random(20);
}
function readRands() {
    itemNum = (itemNum+1)%myArray.length;
    trace(myArray[itemNum]);
}
setInterval(readRands, 100);
  1. var myArray = [];
  2. for (var i = 0; i<10; i++) {
  3.     myArray[i] = random(20);
  4. }
  5. function readRands() {
  6.     itemNum = (itemNum+1)%myArray.length;
  7.     trace(myArray[itemNum]);
  8. }
  9. setInterval(readRands, 100);

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 10 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.