I can't understand how the RANDOM block works

Good afternoon, colleagues. I apologize for my English. I write on the forum through a translator. I have a question about the “RANDOM” block . In my project there are 4 blocks “Flip-Flop” . At the input SET to send a signal from RANDOM. How do I make the random generator output from 1 to 4 . Signal 1=Flip Flop 1; 2=Flip Flpo2;3=Flip-Flop 3;4=Flip Flop 4.

Since there is no built-in “random” node, I assume you are referring to nkrkv/random, which returns a number between 0 & 1. You can either split this into 4 equal parts or multiply by 4 & round to get a number between 1 & 4.

You will need to use logic to determine which flip-flop to toggle based on the random number.
image
One side-affect of this code is that it will not toggle same flip-flop consecutively. If random returns same range twice in a row, flip-flop will only toggle once. This is because TGL only triggers when boolean changes from false to true.

You could add additional nodes to activate flip-flop from same pulse that updates random so the same flip-flop could be changed more than once before any others are changed.


The defer node is only to make sure less/not/and compares have completed before pulse is sent to gate nodes. The “not” of “less” is the same as testing for “greater-or-equal” of the same value, but requires less CPU since we have already done the “less” compare.

It appears random function is not supported in simulator, so I haven’t tested this code.

gweimer thank you very much. Your example helped us understand how it works. Thank you again. We continue to make a game for my nephew

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.