A Flash Developer Resource Site

Page 1 of 3 123 LastLast
Results 1 to 20 of 45

Thread: [Resolved] [Resolved] [Resolved] [Resolved] [Resolved] [Resolved] [Resolved] [Resolved] [Resolved] [

  1. #1
    Junior Member
    Join Date
    Sep 2000
    Posts
    17
    Does anyone use math.random() ( the "preferred" random function in Flash 5 according to the documentation at least ) instead of the older random()?

    It would be relatively easy to write a wrapper around math.random() to retain the identical functionality of the older random() ( namely being able to set a range since it just returons 0.0 to 1.0 ) but I am wondering if it is worthwhile or if the wrapper around math.random() will be slower and thereby less useful then just using the deprecated random(). Has anyone tried this yet?

    Thanks.

    -Hythian

  2. #2
    Senior Member
    Join Date
    Feb 2001
    Posts
    475
    hm... I haven't yet. But your idea sounds interesting. I might have look at this tomorrow...
    I think a wrapper that enables you set a range for the random values will be slower than the normal "old" random function. But i don't know why you consider speed problems with this function? Random numbers aren't needed very often, i think... so calling this function once or twice in a movie wouldn't slow down the playback.

    Yours
    HTD

  3. #3
    Junior Member
    Join Date
    Sep 2000
    Posts
    17
    I want to use it to randomize the elements in an array, and the only way I can think of offhand to do that is this.

    function randomArray (inputArray) {
    newArray = new Array();

    arrayLen = inputArray.length;
    for (i = 0; i < arrayLen; i++) {
    rndNumber = random(inputArray.length);
    tempArray = inputArray.slice(rndNumber, rndNumber+1);
    newArray = newArray.concat(tempArray);
    }
    return newArray;
    }

    Given that it has to walk the length of the entire array in the loop, if Math.Random is faster, even with a wrapper to give me a number in the range I need, then I most definately want to be using it. There may be a more efficient way overall to do this, but none have yet came to mind.

    -Hythian

  4. #4
    Senior Member
    Join Date
    Aug 2000
    Posts
    588
    I have played with the Math.random() method a little bit, and have switched to that over the random() function. Why...I don't know. I guess because it was newer.

    In a couple of experiments i've used it in, it didn't seem to really make a difference. However, I also have not really pushed it very hard either.

    Just my 2 cents.

  5. #5
    Junior Member
    Join Date
    Sep 2000
    Posts
    17
    My bad, I need to fix that code example anyways as I hadn't realized at the time I quickly typed it out that array.slice() didn't remove the section slice'd from the original array so you would get the same values repeatedly, especially using the old random function which is rather non-random ( just trying it, three out of 5 times it returned the same value from a list of 36 choices three times in a row, which is bad since slice doesn't modify the original array ).

    -Hythian

  6. #6
    Senior Member
    Join Date
    Mar 2000
    Posts
    472
    Hey Hythian!

    The new Math.random will no doubt result in a slower clock, due to more complex compiling, but the best reason for it's use is ... it will be around for use in subsequent versions of Flash!

    Here's an example for you, with a random-exclusive result:
    Code:
    function randomArray (inputArray) {
        newArray = new Array();
        arrayLen = inputArray.length;
        for (i=0;i<arrayLen;i++) {
            rndNumber = Math.floor(Math.random()*inputArray.length);
            tempArray = inputArray.splice(rndNumber,1);
            newArray.push(tempArray);
        }
        return newArray;
    }
    inputArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
    randomArray(inputArray);
    trace (newArray);

    Hey HowardTheDuck!

    hehe ... Cool footer, but I'm afraid it's incorrect:
    Code:
    x = 25;
    y = parseInt(x,8); // returns 21
    z = parseInt(x,13); // returns 31
    trace ("y: "+y+" ... z: "+z);
    Richard
    [Edited by Dickee on 05-08-2001 at 09:36 PM]

  7. #7
    proud new daddy! LuxFX's Avatar
    Join Date
    May 2000
    Location
    Dunsinane
    Posts
    1,148
    Here's another variation. Anyone up for some speed runs? I wouldn't mind knowing which is faster.

    Code:
    function randomArray(arrayInput)
    {
    	var temp = new Array();
    	var len = arrayInput.length;
    	for (z=0;z<len;z++)
    	{
    		var rndNum = Math.floor(Math.random()*arrayInput.length);
    		temp.push(arrayInput[rndNum]);
    		arrayInput[rndNum] = arrayInput[arrayInput.length-1];
    		arrayInput.pop();
    	}
    	return temp;
    }
    
    myArray = new Array(1,2,3,4,5,6,7,8,9,0);
    newArray = randomArray(myArray);
    trace(newArray);
    This version uses pop() instead of splice(). I don't know which might be faster.

    cheers!

  8. #8
    Senior Member
    Join Date
    Mar 2000
    Posts
    191
    Actually you are both wrong

    31 OCT = 25
    25 DEC = 31

    thats why!

    a = 31;
    b = 25;
    y = parseInt(a,8); // returns 25
    z = parseInt(b,13); // returns 31
    trace ("y: "+y+" ... z: "+z);

  9. #9
    proud new daddy! LuxFX's Avatar
    Join Date
    May 2000
    Location
    Dunsinane
    Posts
    1,148
    I don't think the Duck was wrong. 31 is the base-8 representation of what 25 represents in base-10.

  10. #10
    Senior Member
    Join Date
    Mar 2000
    Posts
    191
    oh I never did understand all that octabinahexadecimal stuff

  11. #11
    proud new daddy! LuxFX's Avatar
    Join Date
    May 2000
    Location
    Dunsinane
    Posts
    1,148
    don't worry, joe, you were half there.

    all in all, it's a fine joke. anyone else up for another horrible math joke? don't feel bad if you don't get it, this goes way back to the days I was taking math class, and probably only makes sense if you're currently studying limits.

    Why do girls think mathematicians make poor lovers?
    Because mathematicians think there is a limit of X to c
    (say it outloud)

    if you get it, if you don't....

  12. #12
    Senior Member
    Join Date
    Feb 2001
    Posts
    475

    Smile

    Well it's nice that you've helped with the random stuff...

    But now on to my footer-Joke... it is definitely correct. I know that only geeks like me can lough about it, but hey - thats the reason why i use it

    To LuxFX...
    Yours is cool too... say it loud and say it fast, hehe
    We should start a new thread with stupid programmers jokes, shouldn't we?

    what about this one:

    Have you heard about the object-orieented way to become wealthy?
    No !?
    Inheritance!

    Yours
    HTD

  13. #13
    Junior Member
    Join Date
    Oct 2000
    Posts
    21

    Angry what da heck!

    your all wrong!!!
    1 + 1 = 5, thats the solution :biggrin:

    I don't know, what the point!

    31 int the octal system
    = 3x8^1 + 1x8^0
    = 3x8 + 1x1
    = 24 + 1
    = 25 (in the decimal system)

    you math cracks

  14. #14
    Senior Member
    Join Date
    Apr 2001
    Posts
    188
    you guys are all over my head - but you brought up something I wanted to ask is there a way to set a min on a random script - currently I write in frame 1 x=random(10); then in frmae 2 an if statement that states if x<5 go to frame one else do something.

    How else could I write random (5-10);

    thanks
    fleep

  15. #15
    Junior Member
    Join Date
    Oct 2000
    Posts
    21

    Smile

    hä?!
    if you want just random numbers from 5 to 10 you simple write:

    x = 5 + random(5)+1

    (remember:with random(5) you get numbers from 0 to 4, therfor you have to add 1, okidoke?)
    So this term gives you numbers between 5 and 10(mathematic:[5;10])

    -------------

    Originally posted by Fleep2010
    you guys are all over my head - but you brought up something I wanted to ask is there a way to set a min on a random script - currently I write in frame 1 x=random(10); then in frmae 2 an if statement that states if x<5 go to frame one else do something.

    How else could I write random (5-10);

    thanks
    fleep

  16. #16
    Senior Member
    Join Date
    Apr 2001
    Posts
    188
    thanks a million- I knew I was doing it the hard way!!!!

  17. #17
    Senior Member
    Join Date
    Apr 2001
    Posts
    188
    One more question if you have a sec- now is there an esy way to do a random of 5 things without an array- or should I go do some homework on arrays? Thanks again

    fleep

  18. #18
    Junior Member
    Join Date
    Oct 2000
    Posts
    21

    Red face

    more infos, pleaaaaaaze

  19. #19
    Senior Member
    Join Date
    Apr 2001
    Posts
    188
    Ok say I want something to pick a random (5, 8, 3, 6) or a random (blue, green, purple, red) where these are the only choices it can select from. and then according to what it picks it will produce an action - can you easily say random (5,8,3,6); or random("blue", "green", "purple", "red"); without the use of arrays- I am still trying to understand arrays - there is no real purpose ni mind right now- I just thought that this message had such advanced random scripters that I would throw it out there. Thanks for yout time.
    fleep

  20. #20
    Junior Member
    Join Date
    Oct 2000
    Posts
    21

    Question

    simplest way is doing it with an array.
    so you can do...

    myArray = new Array(5,8,3,6);

    ...then...

    x = random(4); //(numbers from 0-3)

    ...then...

    choice = myArray[x]; //if x=2 you get choice=3

    ...hope it helps





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center