# Random Square Wave

Discussion in 'General Electronics Chat' started by Rick Harrison, May 6, 2006.

1. ### Rick Harrison Thread Starter New Member

May 6, 2006
5
0
I am trying to design a circuit that will give me a "random" square wave. I know that I can get "white noise" by reverse-biasing a transistor, but I need to convert that to a lower frequency, irregular square wave that I can read in through a PIC port. It seems like this should be easy to do with an op-amp or some other type of amplifier and possibly a flip-flop, but I'm too much of a novice, especially with analog electronics, to work it out.

Ideally, the circuit would have a pot that could be used to adjust the average frequency from, say, 1,000 Hz to 100 Hz or less. It is not important for the square wave to have a precise 50% average duty cycle, but it can't be way off either (i.e. not a series of tiny pulses with lots of space in between) or I'll have trouble reading it with my PIC. This is why I'm thinking a flip-flop might be needed.

E.G.

,,,|''''''''''''''''''''|,,,,,,,,,,,,,,,,,,,,,,,|"""""|,,,|''''''''''''''''''''''''''''|,,,,|'''|,,,,,,,|''''''|,,,,,,,,|'''''

|<-------------------------------------100 msec-------------------------------------->|

I don't necessarily need to use a reverse-biased transistor as the noise source, but I don't know any other ways (other than using radioactive decay) to get random data. I want the source to be truly random rather than pseudo-random or I could just use the RAND function inside the PIC.

Can anyone point me in the right direction?

Rick Harrison.

2. ### Papabravo Expert

Feb 24, 2006
10,340
1,850
Unfortunately for you circuits are deterministic. This makes them unsuitable for generating random outputs. Pseudo random is as good as it gets.

3. ### Ron H AAC Fanatic!

Apr 14, 2005
7,050
657
Reverse-bias a transistor B-E junction (or use a zener). You can optimize the amplitude of the noise by experimenting with the bias current. Amplify that noise (if necessary) and run it through a comparator. Send the output of the comparator to a toggle flip-flop. This gives you an average 50% duty cycle, but the frequency may be too high, unless you can resample it with your PIC. If you need to, "heterodyne" it down to whatever maximum frequency you want by sending the "50% noise" from the toggle FF to a D FF which is clocked at whatever rate you choose. For example, if you want the minimum pulse width to be 1 second, use a 1 Hz clock. You might want to go through a series of 2 or 3 D FFs to minimize the likelihood of metastability.
This scheme will be slightly unbalanced by unequal setup and hold times, and unequal rising vs falling propagation delays, of your FFs. A 74AHC74 will be better than a CD4013, for example.

4. ### Rick Harrison Thread Starter New Member

May 6, 2006
5
0
Thanks. I think I get it. Yes, I believe the frequency would be too high, but I don't like the idea of sampling the "50% noise" with a clocked flip-flop because it takes too much of the randomness out of it.

Couldn't I use an op-amp based comparator to give me relatively infrequent pulses whenever the noise wave from the B-E junction or zener reached some high level and then run those pulses through a flip-flop to turn that into the square wave that I want? I.E.

NOISE: (&^@#\$(*%!(%#^%@\$^(!*&@#%!@%#&^%!#@(%!@#(%^!@#%!(

PULSES: __|____________|_____|_______________|_____________|_______

WAVE: ,,,|''''''''''''''''''''''''''''|,,,,,,,,,|'''''''''''''''''''''''''''''''''''|,,,,,,,,,,,,,,,,,,,,,,,|'''''''''''''''

To go from noise to pulses, I'm thinking that by adjusting the reference voltage on the op-amp until it just clips the top end of the noise, I could get relatively infrequent pulses. I have a scope (which I sort-of know how to use), so I could try this out.

To go from pulses to a wave, I was thinking of a flip-flop which could be clocked at a high rate, unless there is some way to make a second op-amp or some other analog device do this part.

Thanks again for taking the time to respond.

Rick Harrison

5. ### Rick Harrison Thread Starter New Member

May 6, 2006
5
0
My "diagram" didn't come out right once posted. The state changes on the square wave are supposed to be coincident with the pulses.

Rick Harrison

6. ### Ron H AAC Fanatic!

Apr 14, 2005
7,050
657
Sampling doesn't remove the randomness. Think about it: Each time the clock edge arrives, The D input (noise) has a 50% chance of being either a one or a zero.

7. ### Papabravo Expert

Feb 24, 2006
10,340
1,850
I'm curious to know if band limited noise meets your definition of truly random as opposed to pseudo-random. The sense of pseudo random is that however long the sequence is it must repeat after some number of generated numbers. The amplified reverse-biased transistor won't do that, but if the output is band limited then it seems that the notion of truly random does not apply.

8. ### Ron H AAC Fanatic!

Apr 14, 2005
7,050
657
I am no theorist when it comes to noise, so I don't know about tests for randomness. However, I can see no reason why bandlimiting would exclude randomness. All practical noise generators are bandlimited - even if they go from DC to blue light.
Can you think of any way you could predict the next bit in the scheme I described? I realize that, even if you can't it, doesn't mean that it isn't possible, but I don't think it is.

9. ### bigbigblue Active Member

Mar 15, 2006
38
0
Why not just use another PIC to generate the Square wave?. Most PIC compilers (basic or C - and there are some free ones) have a RAND() function for generating PSEUDO random numbers and some kind of WAIT() function to delay a number of milliseconds.

The C code for doing what you want would be fairly simple, something like :

while(1)
{
wait(rand()*100);
PIN = 1-PIN; // Toggle PIC port pin
}

which would give you PSEUDO random wait of 0 - 100 milliseconds and a random mark space ratio as well.

Regards
and then toggle a PIC port PIN

10. ### Papabravo Expert

Feb 24, 2006
10,340
1,850
This may be just a semantic thing, but the original poster brought up the distinction between "truly random" and "pseudo-random". I believe that a band limited signal is not truly random, because some changes are disallowed. From one sampling instant to the next, no matter how small the interval, the range of amplitudes is restricted. If I make the the band limit and the sampling interval small enough, I can predict the next value of the function with any desired precision that you care to specify.

In order to create a "truly random" process rather than an approximatly random or pseudo random process we need to have a source which is everywhere continuous, but nowhere differentiable. In short it is a function made up entirely of corners. The best example of a corner is the absolute value function evaluated at x=0. The function which is nowhere differentiable allows any slope including vertical which is equivalent to having frequency components out to infinity.

That's my take on the meaning of truly random, yours could vary.

11. ### Ron H AAC Fanatic!

Apr 14, 2005
7,050
657
I defy you to make or buy an infinite bandwidth noise generator, which is what you have defined (as you said). The definitions of noise I found when searching usually included the qualifier "within a specified frequency band", or something similar.
Digital noise, of course, is quantized, and cannot take on values other than one or zero. How about calling this circuit a random number generator, or a random bit generator? I suppose you will tell me that, if I clock it at one gigahertz, and you sample it at 10 gigahertz, you can predict 9 out of 10 samples. I plead no contest.
But I can tell you that, if I clock it at 1 Hertz, and you only sample it every 10 seconds, you will not be able to predict the next bit. Is that concept totally useless? I don't think so.
BTW, I'm not defending "my circuit". I'm just arguing the broader concept of random digital noise generation.

12. ### Rick Harrison Thread Starter New Member

May 6, 2006
5
0
Thanks, everyone, for your help. I'll have to do some experimentation and see what works.

Rick.

13. ### Papabravo Expert

Feb 24, 2006
10,340
1,850
Ron,

In no way did I mean to impugn your circuit or your analysis, and I'm sorry you seem to be taking it that way. My issue is precisely the difficulty involved in defining and quantifying the difference between truly random and any other kind of random. In most cases pseudo random or random in the statistical sense is deemed to be good enough. That appears to be the case with the band limited noise generator in its applications.

Nevertheless the original poster wanted a circuit to produce a truly random output and I maintain that it is an impossible task. Part of this impossibility is that there is no test which can establish true randomness. Close is as good as it gets.

14. ### Ron H AAC Fanatic!

Apr 14, 2005
7,050
657
Papabravo, I didn't mean to sound defensive. I wasn't offended. I apologize for sounding cranky.
I just felt like you were getting off on an "academic" tangent that was not going to be helpful to Rick. He said he doesn't want pseudorandom noise, which this isn't. He said he wants the pulses wide enough that can sample them with his PIC. He wants approximately 50% duty cycle (this obviously has to be the average over a long time). As I said before, if he clocks the noise from the toggle FF into the second D FF at a rate faster than he plans to sample it with his PIC, he will get results that are unpredictable.
There may be a slight bias toward 1 or 0 over the long run, but if he uses fast flip-flops, I would bet that it will be considerably less than 0.1% - probably several orders of magnitude less, depending on how frequently he samples it.
I'm an engineer. I think you are an engineer. We look for solutions. They generally don't have to be perfect - just good enough. I'm guessing that's what Rick needs.

Regards-
Ron Harrison - ain't that a coincidence?

15. ### Rick Harrison Thread Starter New Member

May 6, 2006
5
0
Both of your comments were interesting and helpful. I think I have come up with a method that will work.

It is true that random noise is always band limited, even if one if referring to all of the noise of the entire universe. But if we were looking for absolutes, we shouldn't look in the field of engineering! When people talk about random number generators, they are always talking about a number that is random within some range. For this reason, I find the concept of a "random bit" to be very interesting because it's the arguably the narrowest band that can exist. It's also particularly useful for my application. But even if I attain it, it will be impossible to prove definitively anyway. The best I can do is analyze huge samples and try to find patterns.

It may very well be that converting the noise to a square wave through a raplidly clocked flip-flop and then sampling it with the PIC at a much slower rate will give me good enough random bits. It is wort a try.

If not, all I have to do is add up a bunch of these bits together and take the rightmost bit of the sum - i.e. is the sum is even or odd? It's still not "absolute" since there is a remote possibility that at some particular tempurature, an oscillation could occur in the noise that corresponds to the frequencies that I am using, resulting in a bias toward even or odd sums, but I'll bet it's good enough.