Yet another question about random numbers

mhedgesmhedges Raised on VCSMember Posts: 634
edited October 2015 in Working with GS (Mac)

Hello -

Trying to look for a simple way to generate two random numbers which should not be equal to each other (and without using tables), I created two attributes, Random1 and Random2, and wrote the following code:

Actor Event Handler:

  • Change attribute Random1 to 0
  • Change attribute Random2 to 0

  • Loop Until Random1 =/= Random2:

  • Change attribute Random1 to random(1,3)
  • Change attribute Random2 to random(1,3)

Then Actor1 and Actor2 display Random1 and Random2.

After running, there are instances where Random1 was in fact equal to Random2.

What the heck is up?

Thanks, regards.

Comments

  • jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408

    forgive me if this isn't 100% correct, but i think i worked this out a few years ago, this is from memory. The logic should work, I have an extra attribute used to trigger the function, but the rest should make sense.

    if getRandom=true
    change attribute Random1 to Random(1,3)
    change attribute Random2 to Random(1,3)

    if random1 = random2
        change random2(1,3)
    
    otherwise
        getRandom=false
    
  • mhedgesmhedges Raised on VCS Member Posts: 634
    edited October 2015

    @jonmulcahy , yes, I have another actor to click and change a boolean to true which starts the process perilously described, the the boolean is changed to false again.

    The reason why I used Loop is that:

    if random1 = random2
    change attribute random2 to random(1,3)

    could generate yet another random1=random2.

    Any other ideas (other than cheating myself for forcing random2 to be random1+1 in case of a match)?

  • mhedgesmhedges Raised on VCS Member Posts: 634
    edited October 2015

    OK, I guess I fixed it by inserting a timer. Here's the code for the event handler:

    If ShuffleOn = true then

    Change attribute Random1 to 0
    Change attribute Random2 to 0

    If Random1 = Random2:
    Loop While Random1 = Random2
    Change attribute Random1 to random(1,3)
    Change attribute Random2 to random(1,3)
    End If

    Timer:
    After 0.05 seconds
    ShuffleOn = false

    End If

    It works, but I'm somewhat disappointed I had to insert the timer; I thought the Loop would take care of it just fine.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • mhedgesmhedges Raised on VCS Member Posts: 634

    Sorry, @tatiang, I fixed the title of the thread, as I actually was referring to two random numbers which should not be equal to each other. I read the thread you included above about five to ten minutes ago, though, looking for an answer. B) Thanks for your concern!

  • SocksSocks London, UK.Member Posts: 12,822
    edited October 2015

    @mhedges said:
    It works, but I'm somewhat disappointed I had to insert the timer; I thought the Loop would take care of it just fine.

    I'd dump the loops and timers and rules and just use something like this:

    Change R1 to random(1,3)
    Change R2 to (R1-1+random(1,2))%3+1

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880

    The suggestion @tatiang made will still work. It will look like this:

    When touch is pressed
    Change Attribute: game.Random1 To: random(1,3)
    Change Attribute: game.Random2 To: (game.Random1+random(0,1))%3+1
    

    The above algorithm will always return two random numbers between 1 and 3 but they will never be the same. (And it will be blazingly fast and efficient!)

Sign In or Register to comment.