Random of 1 , -1 ? how?

POMPOM Member Posts: 2,599
edited November -1 in Working with GS (Mac)
hey guys, simple question :
how do i make an integer a random of 1 or -1 ?
if i use simple random like random(-1,1) it has the value "0" in it -> no good for me.
is there a simple way?

p.s
please dont say :
change temp to random(0,1)

if 0 :
change to -1

if 1
change to 1
=)

Roy.

Comments

  • PhoticsPhotics Member Posts: 4,172
    self.random = random (1,2)
    If self.random = 2
    Then self.random = -1
  • POMPOM Member Posts: 2,599
    @Photics
    thanks for the quick reply
    is there no other way to do this in just a simple change rule?
  • PhoticsPhotics Member Posts: 4,172
    I don't see another way...

    min, max? Nope, don't think so.

    Inverse Sine?
    (There's a nice screenshot/graph on my website)
    http://photics.com/books/gamesalad-textbook

    -1 to 1... but it could fall anywhere in there.

    What's wrong with an extra rule?
  • JackBQuickJackBQuick Member Posts: 524
    You could try:

    2*random(-1,0)+1

    (Haven't tried this but, theoretically, it should work.)
  • PhoticsPhotics Member Posts: 4,172
    JackBQuick said:
    (Haven't tried this but, theoretically, it should work.)

    Your theory is sound. I test it and it worked as intended.
  • POMPOM Member Posts: 2,599
    @JackBQuick Perfect! now thats thinking! =)

    @Photics
    Nothing wrong with extra rule, im just trying to learn how expert developer should do stuff..
  • PhoticsPhotics Member Posts: 4,172
    p-o-m said:
    Nothing wrong with extra rule, im just trying to learn how expert developer should do stuff..

    It's good thinking. It's fun figuring out complicated stuff with formulas.
  • BarkBarkCoBarkBarkCo Member Posts: 1,400
    Photics said:
    self.random = random (1,2)
    If self.random = 2
    Then self.random = -1

    For clarity sake, this would give you either 0 or 1.
  • PhoticsPhotics Member Posts: 4,172
    BarkBarkCo said:
    For clarity sake, this would give you either 0 or 1.

    I don't think so.

    I put... Then self.random= -1

    not...

    Then self.random = self.random-1
  • BarkBarkCoBarkBarkCo Member Posts: 1,400
    Sorry, didn't see the If/Then. You are correct.
  • CodeMonkeyCodeMonkey Head Chef, Member, PRO Posts: 1,803
    Not that it matters since you have an answer, but you could use this too.

    (-1)^random(0,1)
  • PhoticsPhotics Member Posts: 4,172
    CodeMonkey said:
    Not that it matters since you have an answer, but you could use this too.

    (-1)^random(0,1)

    An exponent?! Now you're just showing off.

    (-1) to the zero power?

    It works, not sure why though.
  • PhoticsPhotics Member Posts: 4,172
    OK... I did some research...

    ...anything to the zero power is 1.
    ...and something to the power of 1 is itself.
  • POMPOM Member Posts: 2,599
    CodeMonkey said:
    Not that it matters since you have an answer, but you could use this too.

    (-1)^random(0,1)

    This is brilliant !
    so simple but yet very interesting.
    Lets see if anyone comes with some more ideas.. just for fun (and for proof that you might be like codemonkey one day HAHA)
  • JackBQuickJackBQuick Member Posts: 524
    CodeMonkey said:
    (-1)^random(0,1)

    Very elegant.
  • frariofrario Member Posts: 164
    CodeMonkey said:
    Not that it matters since you have an answer, but you could use this too.

    (-1)^random(0,1)

    Lovely!
Sign In or Register to comment.