6
\$\begingroup\$

I want to implement a circuit that generates a random 4 bit using a synchronous counter.

Is it satisfactory to achieve a "somehow" random generator by using a parallel 4-bit counter with the help of the clock speed?

For example, a button is pressed within 5 seconds to display a random number. Within 5 seconds, my circuit have traversed the numbers 0 to 15 at least thousands of times before a state is chosen. With the above circuit, it must be almost impossible for the user to predict the number since even the time spent on clicking another switch for the state to be chosen generates difficulty in prediction.

This is my first time building a physical circuit and I want to know if the above suggestion is bad since my intuition tells me that it is somehow unsafe for the circuit to count thousands of times in every seconds.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ We did this all the time with dynamic RAM. Even on the mainframe STCK (store clock) was used. BUT - it was always the "reverse" pattern that was used as volatility decreases as the flip-floppage increases. \$\endgroup\$
    – mckenzm
    Dec 13, 2021 at 23:07

4 Answers 4

6
\$\begingroup\$

You can indeed use a free-running counter for this purpose.

The randomness is generated by the length of time the user pushes the button, so if the clock rate is significantly faster than the users response time (let's say 10kHz+), then you should get a decent "random" number, with little ability for the user to predict (*).

my intuition tells me that it is somehow unsafe for the circuit to count thousands of times in every seconds

If you think about it, your computer is currently counting billions of times every second with no ill effect (well, I suppose it gets warm). Free-running counters are not unsafe so long as you consider the thermal requirements, which for a 4-bit counter running at a resonable speed (i.e low-MHz range or less as opposed to GHz), will likely be negligible.


(*) They could of course chop the button off and attach a signal generator to make an exact pulse width, thus losing all randomness, but I'm guessing that's out of scope of the question.

\$\endgroup\$
0
2
\$\begingroup\$

There are some ambiguities in your question, but the principle you suggest is sound. Taking the criterion used in athletics, human reaction time is more than 100ms thus a counter running at 100Hz and sampled by human action produces an unpredictable result. Translating this into design criteria, if you run your counter at, say, 1kHz then sample or halt it with a pulse generated by a pushbutton then the result will be entirely random. This applies even if there is contact bounce or if the counter starts from the same value every time. To make it more obvious that the result is random, do not display the counter value until it has been sampled so prediction is clearly impossible.

\$\endgroup\$
2
\$\begingroup\$

For a random result, preventing repeated attempts must be addressed: users should have only one chance. And consider what to do if no button is pressed in the 5-second window, or is pressed before the 5-second window starts.

it must be almost impossible for the user to predict the number

How is the user prompted to start the 5-second period?
Randomness could be improved by "softening" the prompt. For example, and audible prompt of "PING" starts abruptly ("P"). A tone that rises in amplitude more slowly helps defeat reaction time.
Similarly, a prompt of a light turning on can be softened by increasing its brilliance more slowly, to defeat reaction time.

Consider too the button type:

  • Some buttons give tactile feedback, or audibly click: these should be avoided.
  • Consider trapping the counter's value on button-release rather than button-hit.
  • Use a button's contact bounce to your advantage: a button's hit or release may seem like a single event, but electrically, contact is made/broken many times, somewhat randomly.

Most logic-type counters can be clocked at very high rates, before mis-counting or failing to count. Even on a breadboard, clocking rates of a million-per-second should be achievable. So don't be too afraid of moving toward the high-rate end, to aid randomness.

\$\endgroup\$
3
  • \$\begingroup\$ Thank you for your answer. I am actually making some sort of a "gambling" game for our school project. I am thinking of connecting the button to the CLK's input of a 4 D flip-flops so that the state would be recorded upon click and no changes follows after that. \$\endgroup\$
    – Han
    Dec 13, 2021 at 16:30
  • \$\begingroup\$ I'll take your suggestions on the buttons to mind, thank you for that. Btw, the button release action trigger can be implemented by a falling edge trigger, am I right? \$\endgroup\$
    – Han
    Dec 13, 2021 at 16:38
  • 1
    \$\begingroup\$ Broaden your options: A button can be made acitve-high (pressed) or active-low (pressed). So the release-action could be either a falling-edge or a rising edge. In any case, "button-bounce" creates multiple edges, both rising & falling. You can regard this as adding a bit more to randomness. Don't count on it (pun) to give "enough" randomness. \$\endgroup\$
    – glen_geek
    Dec 13, 2021 at 17:23
1
\$\begingroup\$

My piece of advice would be to use flip-flops or shift registers in conjunction with xor gates to create a pseudo-random number generator. This kind of circuit is called LFSR, they have many applications such as generating digital noise on early computer-controlled sound systems and in the past were used even in cryptography. The amount of xor gates to be employed depends on the topology and number of stages. For better results, it would be wise to implement the maximal functions according to the number of stages the device would have.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.