Random Number Generation using C

Thread Starter

San_Dendlek

Joined Mar 24, 2010
11
Hi, I want to generate a random numbers for my PIC 16F84A.
I am using MPLAB to write the program.

My project is basically that I have 8 leds connected to PORTB of the PIC and I want to generate countinously random numbers between 1 and 255.

Any help is appreciated thanks in advance.
 

AlexR

Joined Jan 16, 2008
732
Most C compilers have a library function rand() that generates a pseudo-random number. Read your C manual to see the details of how to use it.
 

braeden

Joined Feb 6, 2008
17
Note sure if this helps but i wrote a c++ program awhile back to generate random numbers. I know your looking for C but perhaps you can make it work.

here you go:

Rich (BB code):
// C++ Random Number Generator


#include <windows.h> 
#include <iostream> 
using namespace std; 

int main() 
{ 
    srand(GetTickCount()); 
    int i = rand()%250; //%250 sets the max number.
    cout << i; 
    cin.get(); 
    return 0; 
}
 

someonesdad

Joined Jul 7, 2009
1,583
The easiest is to use your library's random number generator. There's a huge amount of literature on random number generation. To do your due diligence, consult Knuth's The Art of Computer Programming. The Numerical Recipes in C book contains some cookbook routines for random number generation. I suggest doing a bit of reading on the subject, as a naive user can be dangerous if he/she thinks it's easy to make up their own random number generator. :p
 

Thread Starter

San_Dendlek

Joined Mar 24, 2010
11
Thanks to all of you I found out how to make this. Especially thanks to AlexR and braeden you gave me the idea. I have a beginner backgound in C and C++ i was using the rand() function wrongly.
 
Top