Random number excluding current number?

ElfizmElfizm Member Posts: 489
I have an event that will then choose a random number from 1 to 150.
That's easy, but how would I make exclude it's Oriningal number.

So self.number is 3

Event happens

Change self.number to random(1,150) (but excluding the number 3 as that was what it was previously)

So far I made a rule that has another attribute that changes to the Oriningal number and then after event if that attribute is equal to self.number then it picked the same number so then it's repeated until they are no longer the same.

Question:
Is there away I can out it into a forumula that just excludes the Oriningal number?

Thanks

Best Answers

  • SocksSocks London, UK.Posts: 12,822
    edited April 2013 Accepted Answer
    Try something like this:

    (Random (1,150)+original number)%150

    (Might need a little tuning)
  • CodeMonkeyCodeMonkey Posts: 1,803
    Accepted Answer
    mod(self.Number+random(0,148),150)+1

Answers

  • SocksSocks London, UK.Member Posts: 12,822
    edited April 2013
    mod(self.Number+random(0,148),150)+1
    @CodeMonkey

    I think (correct me if I'm wrong) if self number is 1 and the random selection is 0 then you can get two (or more) 1’s in a row ? My version (essentially the same deal as yours) has the same issue, hence the 'might need a little tuning'.

    . . . unless I'm missing something ?

  • CodeMonkeyCodeMonkey Head Chef, Member, PRO Posts: 1,803
    edited April 2013
    @Socks. that's why there is a +1 at the end.

    Think of it this way...You have the numbers 1 through 150 lined up in a circle. You have your starting number and you want a different number in that circle, so there are 149 other spots in that circle. So you need to add a random number between 1 and 149 to that number. But since a mod function returns a number between 0 and the max number-1, you need to normalize it by first subtracting 1 from the original number then adding 1 back after the mod, so your results will be in the set of 1-150 instead of 0-149.
  • SocksSocks London, UK.Member Posts: 12,822
    Damn, just worked out what you are doing . . . 1 + 0 (random selection output) + 1 = 2 !!

    You're good, very good !!
  • Braydon_SFXBraydon_SFX Member, Sous Chef, Bowlboy Sidekick Posts: 9,273
    edited April 2013
    *puts hunter cap on* Time to go catch a @CodeMonkey for future projects. ;)
  • SocksSocks London, UK.Member Posts: 12,822
    @Socks. that's why there is a +1 at the end.

    Yep! I spotted that just now ! (See post above).

    Simple but clever !
    :)>-
  • CodeMonkeyCodeMonkey Head Chef, Member, PRO Posts: 1,803
    *puts hunter cap on* Time to go catch a @CodeMonkey for future projects. ;)
    There will be a bunch of templates going on Marketplace in the next few months. That's as close as you will get to catching a CodeMonkey. Sorry.
  • ElfizmElfizm Member Posts: 489
    @socks and @codemonkey

    Thanks, however, How can I type in the "mod" bit?
  • Braydon_SFXBraydon_SFX Member, Sous Chef, Bowlboy Sidekick Posts: 9,273
    @Elfizm - why not just choose it from the drop down menu inside the expression editor?
  • ElfizmElfizm Member Posts: 489
    @Braydon_SFX

    That's where I though it would be, but it's not nor anything similar. My guess is that because, for the time being I am on 0.92 I may not have it?!
  • CodeMonkeyCodeMonkey Head Chef, Member, PRO Posts: 1,803
    Oh wow that is old. Try this instead. This is the only time I'll recommend % over mod.

    (self.Number+random(0,148))%150+1
  • ElfizmElfizm Member Posts: 489
    @CodeMonkey it is very old, but until my new Mac arrives, i am just playing around on this on.
    Works quite nicely now thank you, I tried altering @socks but it was working. Thank you both
Sign In or Register to comment.