Shop OBEX P1 Docs P2 Docs Learn Events
SX/B Random for jack-in-the-box — Parallax Forums

SX/B Random for jack-in-the-box

scary terryscary terry Posts: 24
edited 2007-05-28 22:52 in General Discussion
Hi All,
I'm building a Halloween prop that will be a large jack-in-the-box. The user will turn a crank on the side of the box that will send pulses to the SX which will trigger the play back, individually, of the 30 notes in the jack-in-the-box song from a ChipCorder. For scare value, instead of popping the "jack" up at the usual place in the song, I'd like to do it randomly.

What I'm thinking I'd like to do at the start of each sequence is generate a "random" integer between 5-30 (starting at 5 notes into the song so they think it's a normal jack-in-the-box), the SX would then count the pulses from the crank and when it got to this "random" number, trigger the "jack" to pop-up.

What I don't know how to do is to generate this integer between 5 and 30. I've looked at the RANDOM command in SX/B and somewhat understand it, I just don't know how I'd get the result to be an integer between 5 and 30. Any suggestions? This will be my first real SX project so I'm at the beginner level in using SX/B.

If anyone has a better way to do this, I'm open for ideas.

Thanks.

Terry

Comments

  • JonnyMacJonnyMac Posts: 9,214
    edited 2007-05-28 16:53
    You can use a bit of math to get a random value to fit into a specific range, here's how I do it:

    result = rVal // 26
    result = result + 5
    


    Remember that SX/B only allows one operator per line, so it takes two lines to do what you might do in one with PBASIC. The first line takes the modulus of your random value, returning a result of 0 to 25 (the span of your range). The next line bumps the minimum and maximum points to re-align to your desired output.
  • scary terryscary terry Posts: 24
    edited 2007-05-28 17:55
    Hi Jon (or should I call you Chuckie's Daddy?),

    Can you expand a little on how you arrived with the value of 26. I assume that's the range from 0 to 25 but I don't quite understand how that works with the Modulus. (I haven't used Modulus in the past and I'm just barely understanding the concept.)

    Also, rVal will be the random number between 0 and 255 generated by the RANDOM command given rVal is a byte. Is this correct?

    Thanks,
    Terry
  • BeanBean Posts: 8,129
    edited 2007-05-28 21:43
    Terry,
    Think of modulus as remainder. So you take the value and divide by 26, returning NOT the result of the division, but the remainder of the division.

    Sounds like a cool project, I'd love to see some video of it after you get working.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    “The United States is a nation of laws -· poorly written and randomly enforced.” - Frank Zappa

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com


    Post Edited (Bean (Hitt Consulting)) : 5/29/2007 11:35:16 AM GMT
  • JonnyMacJonnyMac Posts: 9,214
    edited 2007-05-28 22:29
    The key to remember with modulus ( // operator ) is that it will always return a value between zero and the divisor-1. As you need a result of zero to 25, 26 is used as the divisor. Yes, rVal is a byte and should be kept as a separate variable.
  • scary terryscary terry Posts: 24
    edited 2007-05-28 22:52
    Thanks Jon and Bean, it took me a while to get it but it makes sense now. I'll be purchasing parts this week and hope to have it operational within a month. I will post videos.
    Terry
Sign In or Register to comment.