Kyma Forum
  Tips & Techniques
  not quite random

Post New Topic  Post A Reply
profile | register | preferences | faq | search

next newest topic | next oldest topic
Author Topic:   not quite random
phillipm
Member
posted 03 June 2013 14:20         Edit/Delete Message   Reply w/Quote
I am looking for a way to generate a not-quite-random series of numbers. Specifically, I want to randomly choose between 9 possible integers (0 1 2 3 4 5 6 7 8) ... that's the easy part.

BUT ... I never want the same number twice in a row. With such a small universe of random numbers to choose from, this happens quite a lot with true random selection.

I've run into this situation before always resorted to a "brute force" solution of some sort. But I'd like to figure out a true solution this time, if possible.

(Sheesh ... someday I hope I will actually learn smalltalk/capytalk. Maybe when I retire.)

IP: Logged

SSC
Administrator
posted 03 June 2013 17:05         Edit/Delete Message   Reply w/Quote
If you always add a random amount to your previous selection, and you make sure that the amount you're adding is never 0, then it should give you a sequence without repeats.

code:

| prev |

prev := EventVariable new.

(prev <~ ((1 s tick sampleAndHold: ((prev + (!Random abs * 7) + 1) truncated)) mod: 9))


By adding a random integer from 1 to 8 to your previous value you should avoid repeating a number. Then you could take the new sum modulo 9 to keep it within the range you specified.

IP: Logged

JulianLeviston
Member
posted 22 November 2013 02:31         Edit/Delete Message   Reply w/Quote
Carla, do you keep these examples and add them to the expressions library on next release? I reckon that'd be an awesome thing, coz this would be a prime candidate, no?

IP: Logged

SSC
Administrator
posted 22 November 2013 09:14         Edit/Delete Message   Reply w/Quote
Thanks, Julian, that's a great idea! Now just have to figure out which category it belongs under...

IP: Logged

All times are CT (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply

Contact Us | Symbolic Sound Home

This forum is provided solely for the support and edification of the customers of Symbolic Sound Corporation.


Ultimate Bulletin Board 5.45c