Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Random Number generator using white noise

Status
Not open for further replies.

Roshan8051

New Member
IMG.gif
I am complete noob in microcontroller programming.
Just need layout of how to use 8051 in producing random numbers based on white noise.
In the attachment is the basic white noise generator circuit connected to opamp for getting square wave for the analog random noise.
Now I need to know how will our 8051 detect 1 or 0 logic levels and display it?
Thank you.
 

misterT

Well-Known Member
Most Helpful Member
Now I need to know how will our 8051 detect 1 or 0 logic levels and display it?

I'm not sure what kind of help you need.. are you asking how to program the 8051 microcontroller?
The microcontroller input pin detects logic levels based on some threshold voltage levels. Simply put, near 5 volts (operating voltage) are logic 1 and near 0 volts are logic 0.. you just read the input pin in your program and that's it.
There are many ways of displaying things.. what kind of display you want to use?
 
Last edited:

MrAl

Well-Known Member
Most Helpful Member
Hello,

Do you need real white noise or can you make due with pseudo white noise. If you can use the latter then there are algorithms that you can use to generate that white noise right within the uC chip itself without the need for an external circuit. The pseudo white noise acts very much like the real thing.
 

MikeMl

Well-Known Member
Most Helpful Member

MrAl

Well-Known Member
Most Helpful Member
Can you help me with algorithm sir?

Hello again,


Well, it starts with a hardware circuit using XOR gates. The logic statements for these gates are then entered into the uC in the required code. When the program is run, it simulates the logic gates and outputs a random number.

The required connections for the logic gates can be found on the web. I would have to look it up to determine the code but if you care to do this then go ahead and try first. If you have problems i'll do a search and see what i can find.
The logic is pretty simple though because the XOR gate is not hard to write out in Boolean logic or even more simple computer code as i'll show with this small pseudo code block...

rem For inputs a and b and output c we have:
c=xor(a,b)

Or when we dont have the xor function available:
c=0
if a=1 and b=0 then c=1
elsif a=0 and b=1 then c=1
end if

Or alternately:
c=0
if (a=1 and b=0) or (a=0 and b=1) then c=1

Or in boolean logic (where '*' indicates logic AND and the apostrophe represents the NOT function):
c=a*b'+a'*b

unless of course you have a built in XOR logic function (shown by an 'x' here):
c=a x b

So the idea is to find the hardware connection you prefer (depending on how many 'gates' you want to use and the max speed) and then write out the logic statements, then program it into the uC chip or into memory of some kind.
 

misterT

Well-Known Member
Most Helpful Member
What is the application? Why you need a random number generator for? A random number generator can be as simple as a counter and a push-button as the random event..
 

unclejed613

Well-Known Member
Most Helpful Member
see if you can find a copy of the TTL Cookbook. it's probably long out of print, but had a chapter on RNG's. the RNG's in the book consisted of long chains of shift registers and xor gates. if you at least know the logical structure of an RNG, you can reproduce it in any logic family, or in PIC code. you could use the circuit in post#1 to preload the shift registers.
 

Roshan8051

New Member
Appreciate your help sir.
But basically I failed to understand the use of XOR gate in this project.
What my idea is ,the output of white noise generator circuit i provided will be in the form square wave.Then can't we directly feed that to uC and program it to give corresponding logic levels i.e 1 or 0 ?
Just 4 days back i learnt about uc pins and we are supposed to build project.So I have no idea.
please help.
 

rohit_cuet

New Member
Improved Zero Text Watermarking

hello ,
i am doing final year project on text watermarking


i need Improved Zero Text Watermarking algorithm anainst meaning preserving attacks source code.
please help me.
 

MrAl

Well-Known Member
Most Helpful Member
Hi again,

Here is a link for a typical writeup on these random number generators
**broken link removed**

Note that you have to follow the text carefully for the connections of the XOR gates or else you could end up with a very short cycle even with a large number of gates and shift registers. Of course the gates and shift registers are emulated in software though for a purely software solution. That's not hard to do either.
There are also better references on the web, that's just one to get started with.

To test the randomness a simple test can be employed. Using a circle of radius R, choose some convenient R value. We can then calculate 'r^2' (lower case R) with:
r^2=x^2+y^2

and solving for r we get the simple:
r=sqrt(x^2+y^2)

The idea is to generate random samples using your pseudo random number generator, to generate x and y samples where x and y can equal 0 to R. Now with x and y being random from 0 to R, if the generator is truly random then the samples should distribute evenly within the square with sides equal to R, and that means that they should also distribute evenly within a circle of radius R. Knowing this, that means that the ratio of the number of samples that are found inside the circle (above) to the number that are found in the square should be equal to the area of the circle divided by the area of the square:
Nc/Ns=(R^2)*pi/(4*R^2)

where Nc is the number inside the circle and Ns is the number inside the square,

and from inspection we can see this is simply:
Nc/Ns=pi/4

So the number of samples that fall within the circle divided by the number of samples that fall within the square is equal to the constant pi=3.1415926.... divided by 4. So the closer we get to pi/4 the more random our generator must be.

To calculate Ns is easy because that is just the total number of samples generated. To calculate Nc however we have to test the sample to see if it is inside the circle. To do this we use the circle above:
r^2=x^2+y^2

and it is clear that if the point generated (x,y) is within the circle then:
(x^2+y^2)<=R^2

so that would get counted as both a sample inside the circle and a sample inside the square. But if we instead find that to be false, then the sample lies within the square only and not within the circle so that only gets counted as being inside the square only.

So the code would look something like this:
Nc=0
Ns=0
R=100
R2=R*R

x=rand(R)
y=rand(R)

loop 1 to 1000000:
Ns=Ns+1
if (x^2+y^2)<=R2 then Nc=Nc+1
end loop.

So we count 1 more Ns for each loop but only count one more Nc if x and y pass the test.
After we do a large number of samples this way (like 100000 or more) we then calculate:
ratio=Nc/Ns

and compare that result to pi/4 and see how close we got. The closer 'ratio' comes out to pi/4 the more random our generator is. If we multiply the ratio we get which is Nc/Ns by 4 we get:
pi=4*Nc/Ns

so it is easy to compare to pi. If we get a ratio times 4 that is between about 2.9 and 3.3 we have a fairly decent random generator, but if we get a number for ratio times 4 that is more like 1.5 or 4.5 then there must be a systematic error in the number generator so we should use a different connection for the XOR gates.

Here is an example of a test run on a random number generator using R=1000 and 1000000 generated x and y samples:

3.141592654 (the constant pi for reference)
3.14164 (the actual result from the circle square test)

so this test came out pretty close. A second run with the same generator produced the result:
3.141592654 (reference)
3.139636 (result)

so this came out a little lower but still close.

For another test, a systematic error was introduced into the generator and the result was:

3.141592654 (reference)
3.995944 (actual result)

and here we see the number is quite a bit different than pi.
 
Last edited:

unclejed613

Well-Known Member
Most Helpful Member
an XOR gate is a binary multiplier, or in the case of a random number generator a "selective inverter". if the two inputs are identical you get 0, if the inputs are different, you get a 1.
 

blueroomelectronics

Well-Known Member
hello ,
i am doing final year project on text watermarking


i need Improved Zero Text Watermarking algorithm anainst meaning preserving attacks source code.
please help me.

Since it's your final year project aren't you expected to write the code yourself? Are you going to give us credit in your submission?
 

WTP Pepper

Active Member
Google "PRBS generators" or "max length sequence generators". Depending on how random you want will help you choose the sequence length (how long it takes to repeat). 32 bit will give a good sequence length to start with (4+billion cycles) on a micro running at 10MHz. The time taken to implement the PRBS will have to be taken into account depending on the speed you need to generate these numbers (good thing is the instructions to do such are pretty simple).

Very easy to implement in software. You can seed these if you have access to a temperature sensor as some 8051 derivatives have on-chip and seed them with the current temperature. An external LM35 on an ADC input will also suffice as a temp detector.

Seeding them is to provide a random number to start with. If you seed them at start up with the same value, then you get the same sequence everytime you power up, but with other external factors, that may be fine for your application.

As said, a push button pressed after startup adds sufficient entropy of the seed as to all purposes, the time in (say microseconds) the button is pressed after reset is unpredicatable as it relies on a human input. If mains powered, you can use a cut down mains signal and count the number of microseconds between cross overs. 50/60Hz is accurate over a long period of time (hours wise) but instantaneous measurements can vary hugely and near randomly which is what you want.

What you have drawn is feasible if you use the input edges to pre-load timer counters. However depending on your speed of the micro, you may need to bandlimit the speed of those edges as they may occur faster than you can deal with them hanging your CPU is interrupt routines you cannot get out of.

If you need to display them on a 16x2 display, then the entropy from the LCD display will be fine if you need to shown them every second.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Top