Random Numbers, No Repeating

cBrookscBrooks Member, PRO Posts: 40

Hey guys, I am trying to make a random number generator for my whack-a-mole game. It needs to generate a number from 1-4. The number generator itself is easy, but getting it to not repeat itself is very difficult for the situation I am in. I was wondering if I could get some help.

Ok, so I followed tshirtbooth's tutorial and it works great when I set it to generate a random number when I click the actor, and it does this without repetition of the previous number. The problem is, I want it to generate a random number whenever a whack-a-mole is hit. I do this by making a game.pressed attribute, and this is activated for 0.1 of a second after each mole is hit.Then I tell the random generator code (which is in another invisible actor) to activate when game.pressed = true instead of when it is touched. It works like I want it to but for some reason it still often repeats itself. If you have a solution, please comment. BTW, if this is too hard to understand ask me what you need to know in the comments and I will answer to the best of my abilities.

Comments

  • daaddddaaddd Member Posts: 116

    I don't know if i really get the idea but i'll give it a try, you said random 1-4 right? create 4 integer attributes as the random will stock each value on this 4 attributes, when the random set a number to the first attribute make a rule that is the first attribute is stocked with "4" the next should be random(1,3) , hope it helps

  • SocksSocks London, UK.Member Posts: 12,822
    edited May 2014

    mod( self.X +random(1,3),4) . . . . . +1

    EDIT: quick example - click square to generate a new number:

    https://www.mediafire.com/?q1gcc606a1x7yo9

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,879
    edited May 2014

    Here is a generic formula that will generate random numbers (without repeating the last number):

    (currentRndNum+random(0,upperLimit-2))%upperLimit+1

    so your specific case would be:

    Change Attribute: game.currentRndNum To: (game.currentRndNum+random(0,2))%4+1

    Edit: Looks like @Socks beat me to it .... again. His solution is functionally equivalent to this one.

  • cBrookscBrooks Member, PRO Posts: 40

    Thanks so much guys, I got it working.

  • cBrookscBrooks Member, PRO Posts: 40

    @RThurman‌, How would I adjust your '(game.currentRndNum+random(0,2))%4+1' so it works with 9 random numbers, 8,10 etc?

  • SocksSocks London, UK.Member Posts: 12,822
    edited May 2014

    @cBrooks said:
    RThurman‌, How would I adjust your '(game.currentRndNum+random(0,2))%4+1' so it works with 9 random numbers, 8,10 etc?

    9 . . . (game.currentRndNum+random(0,7))%9+1

    8 . . . (game.currentRndNum+random(0,6))%8+1

    10 . . (game.currentRndNum+random(0,8))%10+1

  • Braydon_SFXBraydon_SFX Member, Sous Chef, PRO, Bowlboy Sidekick Posts: 9,271

    I also have a method that uses tables. I've posted it in the spare code thread.

  • cBrookscBrooks Member, PRO Posts: 40

    Hey thanks so much @socks

Sign In or Register to comment.