Creating a one-off random value?

Official support for: sonicbirth.sourceforge.net
Post Reply New Topic
RELATED
PRODUCTS

Post

At a certain moment (say, every time a timer reaches X) i'd like to generate a random number. So the number wouldn't continuously change, it'd just change once in a while. Has anyone done something like this already? How did you approach it?

Thanks

Post

Yikes. A random number . . . I think you would have to go outside SB for that although it could be added to the code as a function. I think random numbers in code like C++ are generated from the computer's clock. There must be some algorithm to do that. Maybe you could replicate it in SB.

I suggest, you run a timer continuously. It never stops. Let that be your "clock." Then you have this other timer that keeps getting to some number and then resetting and so every time that second timer gets to that number you use a comparator to pass some complex math operation on a digit grabbed from the master clock (the continuous timer).

ie: when the output of timer < 4 (arbitrary choice) pass along this value grabbed from some operation on the master clock. Use something like modulo, remainder or any of those functions that can grab a digit from the master clock's value at that moment. There is probably some info you could find online for a series of functions that could take a clock value and make it look random.

This is interesting and I can see it having some good uses.

Post

Thanks, that looks like a good approach. i'll give it a try.

Post

I guess i can put the difficulty i'm having in more general terms:

Say i have two timers (t1, t2). Downstream from the timers is a subcircuit (s).

I want s to receive a number derived from t1. But I want this number to be 'sampled' anew each time t2 reaches 10, and I want s to continue to be fed this same number (even though t1 is continuing) until the next time t2 reaches 10, at which point a new value is grabbed from t1.

I'm having trouble figuring out how to create this kind of persistence. Perhaps there's something simple i'm overlooking...

Post

Well, it turns out that SB has a random number generator built in. We didn't need to do any of this. Here's how to generate random numbers between 1 and 10.

From the generators menu, choose random. Send it into an absolute value module (algebra menu). Multiply that my 10 and then use the nearest integer function. Done.

Anyway, before I noticed that I figured out how to solve your "sample and hold" persistence problem two ways, one using the two timers and a feedback loop and a nearest integer function. Another, simpler more CPU efficient way using just one timer, multiplying it by a slider value as a kind of clock control speed, and then using the nearest integer function.

Although not needed for the random numbers, these circuits could be useful later for something else. Let me know if you want them.

Post

dscro wrote:Anyway, before I noticed that I figured out how to solve your "sample and hold" persistence problem two ways, one using the two timers and a feedback loop and a nearest integer function. Another, simpler more CPU efficient way using just one timer, multiplying it by a slider value as a kind of clock control speed, and then using the nearest integer function.

Although not needed for the random numbers, these circuits could be useful later for something else. Let me know if you want them.
Ah! i missed this reply somehow. I'd love to take a look at your sample and hold solutions. If possible could you send the you can use: sbirth.10.cbit@spamgourmet.com (disposable email address, to avoid being spammed) cheers!

Post

Sure. I'll send them tonight when I get home.

Post Reply

Return to “SonicBirth”