Random Number Generation using C

Thread Starter


Joined Mar 24, 2010
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.


Joined Jan 16, 2008
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.


Joined Feb 6, 2008
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() 
    int i = rand()%250; //%250 sets the max number.
    cout << i; 
    return 0; 


Joined Jul 7, 2009
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


Joined Mar 24, 2010
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.