Continue to Site

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.

A Random Question

Status
Not open for further replies.
2camjohn said:
Using PICs: What methods can I use to generate a random number?

What are the advantages and disadvantages of the methods in terms of Processing cost and randomness of number.

You don't generally get 'random numbers', your PC can't do it either!.

What you usually get is a sequence of numbers based on a mathematical formula - in order to start it you need a random seed value, if you start with the same seed value you will get the same sequence. You then have the problem of generating a 'random' seed value to seed your 'pseudo' random number generator. Rather like the 'chicken and egg' question :lol:

How I usually do it is to use a PIC timer running very fast, the PIC program waits for a key press - when the key is pressed the random seed is initialised with the value from the timer registers.

As usual, look on the PICList for examples! - or in the PIC Source Code Book, available free online at Dontronics (which is the one I use).
 
One of my favorite instances of the "pseudo" in pseudo random number generators comes from Las Vegas. A group of techies watched all the numbers in a game called keno and figured out which random number generator was being used. They used this to compute the next 6 numbers and win getting 6 out of 6 numbers. Unfortionately for them no one had ever gotten all 6 numbers and given that they won the first time they ever played the game it looked really suspect and they were caught.

You can sample the noise coming from a diode or resistor which idealy is random.

You could use uninitialized Ram locations for your seed. At startup, ram takes on fairly random patterns.
 
2camjohn said:
Using PICs: What methods can I use to generate a random number?

What are the advantages and disadvantages of the methods in terms of Processing cost and randomness of number.

Thanks
John
what is the definition of random..sounds like a good thread title...i woudl say never can be repeated, right...
well this is my random number generator---i had a 20MHz IC clock hooked up to four counters.. when a button was held down the clock was connected to the counters, when the button was let go the clock was disconnected from the counters, and LEDs would show a pattern..
due to the fact that you could never push the button (on) for the same amount of time the resulting pattern would allways be different..i used twelve LEDs three groups of four..
 
This is what I was worried about.

I know that on linux you can generate true random numbers but it requires user input (say from the mouse).


I was hoping there would be a simple way of generating a quite random number (just an even spread between two values would be OK) without needing input from the user.


bmccula:
How would I go about sampling noise from a resistor or diode?


Thanks
 
2camjohn said:
This is what I was worried about.

I know that on linux you can generate true random numbers but it requires user input (say from the mouse).

It doesn't sound like a true random number at all, it sounds exactly the same as other systems, either Windows or DOS - just a psuedo random sequence, requiring a random seed to start.

I was hoping there would be a simple way of generating a quite random number (just an even spread between two values would be OK) without needing input from the user.

What exactly are you trying to do?.

bmccula:
How would I go about sampling noise from a resistor or diode?

The usual method is to use a zener diode, try googling for 'white noise generator' - although I'm not sure how you will generate a number from the white noise?.
 
If I remember correctly diodes create noise based on their temperature. The noise probability curve is a gaussian curve. If you roll dice the odds for a number comming up are the same for each number - a flat probability curve. With a gaussian curve the values in the middle are more probable than the values on the edges. This is the type of curve you get when you roll 2 dice and add the numbers together.

This noise is truly random becuse it results from the random movement of electrons due to heat.

As far as making a random number you just amplify the noise from the diode and send it to an ADC. I'm no expert on this though. If you spend a bit of time with google you probably will find a fair amount of info on noise generation. There is a device called a "noise diode" that is designed for noise generation - might be a good place to start your search.
 
I think that the best and simpler...

For any micro running fast (4 MHz should be -is - more than enough) and a user pressing two or more buttons, an 8-bit counter running continuously in the background should give random numbers. Yes, they are just 256 possible ones (not a random thing) but which one, that's random.

I used that idea in a design for training in martial arts where the trainee had to kick random lighted pads with the next always lit at random based on when he did the previous kick.

Not a big deal in code size or RAM involved. Sure nobody, even Bruce Lee, could predict which one would be next!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top