A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: Neg. Pos. randomizer.

  1. #1

    Neg. Pos. randomizer.

    Hey,

    I was trying to figure out how to make a number randomly positive or negative... I know how.. but it wont work...

    Ive come to it, that I have to create a random number of either 1 or -1 and multiply it by my set number already... But I cant figure out the code to get that -1 or 1... Any ideas?

    I tryed

    L=L*Math.floor(1-Math.random()*3);

    Didnt seem to work well.. It gave me all negative numbers...

    Thanks to all in advance!

    Peace,
    ~Bill
    So many Ideas

    So little Time

  2. #2
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    could do this...

    negpos = random(2) ? 1 : -1;
    L = L * negpos;

    ...or...

    L = random(2) ? L : -L;



    Last edited by Lexicon; 07-22-2004 at 04:27 AM.
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  3. #3
    flash mangler drunkenmaster2000's Avatar
    Join Date
    Apr 2001
    Location
    England
    Posts
    156
    or...

    L = L * Math.ceil(Math.random(2))-1;

  4. #4
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    but that will always give 0! :O

    even if you changed it to....

    L = L * Math.ceil(Math.random()*2)-1;

    you will get random 1 or 0 and not the required 1 or -1!
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  5. #5
    flash mangler drunkenmaster2000's Avatar
    Join Date
    Apr 2001
    Location
    England
    Posts
    156
    oopsie

    L = L * Math.ceil(Math.random()*2)-1;

    that *is* what I actually meant.

    And yes, it's still wrong

    L = L * Math.floor(Math.random()*2)-1;

    that should do it though....



    right?

    edit: no, that's still not right, is it? That would give you -1 or 0.

    let me think....

    ah, nuts to it, your way seems to work, so I'll just agree with you.

    I've just never used the random(n) and don't know the syntax. I've always used Math.random(n), which seems to work in a totally different manner.
    Last edited by drunkenmaster2000; 07-22-2004 at 07:04 AM.

  6. #6
    Senior Member Genesis F5's Avatar
    Join Date
    Jan 2002
    Location
    Unallocated memory
    Posts
    1,845
    num=10; //ENTER A NUMBER THAT WILL BE THE LOW/HIGHEST POSSIBLE NUMBER.
    newnum=Math.round(Math.random()*num-(num/2))

    2 will be the lowest. It will return -1,0 and 1. You can't just get it to output either -1 or 1 without more extra code than it's worth. Just use an "if" statement to guard against using a zero if one is generated.

    or another possible solution:

    num=10; //ENTER A NUMBER THAT WILL BE THE LOW/HIGHEST POSSIBLE NUMBER.
    newnum= Math.round(Math.random()*num-(num/2))
    while(newnum!=0)
    {
    newnum= Math.round(Math.random()*num-(num/2))
    }


    I hope this helps.

    -genesis f5 (mx)

  7. #7
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038



    L = random(2) ? L : -L;

    is the same as

    L = Math.round(Math.Random()) ? L : -L;


    random(20) creates a random integer from 0 to 19.
    Math.random() creates a float (that has decimal places) from 0 to 1
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  8. #8
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    yes Genesis, which is why I posted the first working solution.
    Which is basically a very simple if else statement. Not really any un-neccessary code!

    L = random(2) ? L : -L;
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  9. #9
    flash mangler drunkenmaster2000's Avatar
    Join Date
    Apr 2001
    Location
    England
    Posts
    156
    Originally posted by Lexicon



    L = random(2) ? L : -L;

    is the same as

    L = Math.round(Math.Random()) ? L : -L;


    random(20) creates a random integer from 0 to 19.
    Math.random() creates a float (that has decimal places) from 0 to 1
    cool. But the bit I really didn't get was the:

    ? L : -L;

    I've seen this before on one flash forum or another, and I never got round to working out what it does. Any chance of a quick explain?

    PS. AS is my first stab at coding since 680x0 assembler (which I wasn't great at, either). I've got my head round a lot of it, but I've never had to use this particular one.

  10. #10
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    the use of ? : is known as a conditional statement

    A conditional statement is made up of 3 expressions.

    expression1 ? expression2 : expression3

    if expresion1 is true (i.e. true, or 1 (or not 0 in flashMX)) then expression2 is returned.

    if expression1 is false (i.e. false or 0) then expression 3 is returned


    so if I trace a conditional statement...

    trace( 1 < 3 ? "this is true" : "this is false" );
    // "this is true" will be returned //

    trace( 1 > 3 ? "this is true" : "this is false" );
    // "this is false" will be returned //

    this is useful for replacing if else statements where you are only assigning one variable depending on another variable.
    Code:
    e.g. 
    if (x < y) {
        isSmaller = "yes";
    } else {
        isSmaller = "no";
    }
    
    //// is basicallly the same as 
    
    isSmaller = x < y ? "yes" : "no";
    so in that respect

    Code:
    L = random(2) ? L : -L;
    
    // is the same as saying
    
    if (random(2)) {
        //random(2) must equal 1
        L = L;
    } else {
        //random(2) must equal 0
        L = -L;
    }
    you can also nest these statements

    Code:
    e.g. 
    if (x < y) {
        relation = "x is LT y";
    } else if (x > y) {
        relation = "x is GT y";
    } else {
        relation = "x and y are the same";
    }
    
    //// is basicallly the same as 
    
    isSmaller = (x < y) ? "x is LT y" : (x > y) ? "x is GT y" : "x and y are the same";
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  11. #11
    flash mangler drunkenmaster2000's Avatar
    Join Date
    Apr 2001
    Location
    England
    Posts
    156
    excellent stuff, lexicon, thanks.

    well explained too.

    Looks like another bout of code-tidying for me tonight then

  12. #12
    flash mangler drunkenmaster2000's Avatar
    Join Date
    Apr 2001
    Location
    England
    Posts
    156
    excellent stuff, lexicon, thanks.

    well explained too.

    Looks like another bout of code-tidying for me tonight then

  13. #13
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    no worries.

    One note though, I once went through a whole load of code and changed to use conditional statements instead of if statements.
    I expected that because I was reducing the actual code itself i.e. 5 lines into one etc. that the byte size would drop. When I looked at the reduction in bytes I was suprised to see that the bytesize actually got bigger! Only by a couple of bytes mind, but you have to question whether spending the time converting old code is actually worth it
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  14. #14
    Banned NTD's Avatar
    Join Date
    Feb 2004
    Posts
    3,438
    Nice explanation of nested conditional statements! Learn something new everyday....... and eventually your head will explode! I learned something new today, thanks.

    NTD

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