Hello together.
I would like to write a program that give me a random number (range: no matter). I can't realize it. I use PIC controllers, may that could help you to give me an answer. Program languages are C or BASIC.
Please help me. Thank a lot!!
Hello together.
I would like to write a program that give me a random number (range: no matter). I can't realize it. I use PIC controllers, may that could help you to give me an answer. Program languages are C or BASIC.
Please help me. Thank a lot!!
Hi Christian,
I hope you are doing well. It's important to realize that computers can't generate true random numbers, because they are deterministic. Every piece of code you will ever find for random number generation will generate a Pseudo Random Number, within a limited range the probability distribution will be approximately flat approximating randomness.
With that said just use the rand() function included with C.
At the top of your code
#include <stdlib.h>
This will include the standard library header which includes the rand() function. I can't help you much more than that because my PIC programming is a little rusty and I don't want to mess you up. This should take care of your problem.
Also you'll have to seed this function if you want to get a different series of random numbers every time you run the application. If you google the library or the rand() function you should find what you need.
hth,
Jorge Garcia
I was feared that I get this answer!
But i have the idea to solve the problem with a hardware solution.
I'll connect a capacitor to an analog input, load it, stop loading, unload it (with resistor), wait a few ms, and read in the voltage. May the discharge level is irregular enough to get a random number?!
What do you think?
Hi Christian,
I agree that the only way to get a true random number is with an analog solution. The RC circuit idea however will not work. RC discharge follows an exponential function which is very predictable.
The implementations of random value generators I've seen used diodes. Try googling random number generation, you should be able to find some information on using diodes to generate random numbers. Be warned these devices are not easy to make, because the random component is a very low-level signal and you'll have to do some processing in order to get a useable value.
hth,
Jorge Garcia
Hi Christian
Closet to a Pseudo Random Number Generator that I know of is the following white noise generator code that I have used
Registers required
Lobyte
Midbyte
Hibyte
Code
movlw 0x0A
movwf Lobyte
movwf Midbyte
movwf Hibyte
Loop
rlf Lobyte,f
rlf Midbyte,f
rlf Hibyte,f
movlw 0xA1
btfsc STATUS,C
xorwf Lobyte,f
movfw Hibyte
movwf GPIO ;For demonstration purposes White Noise will be on GPIO,0
goto Loop
Regards
Graham
Works well for white noise, and a generated random number on any of the bytes
Hi Christian
Maybe something like this (elektor magazine) connected to an analogue input, most likely after some amplification, will give you a signal random enough to satisfy your requirements. If you want I can do up a simple picture of the circuit from the magazine and post it here (don't have it with me at the moment)?
Hope this helps.