PDA

View Full Version : creating pseudorandom number in PIC C


shwaip
16 Jul 2005, 12:29am
I'm working on a project for a PIC 16F87, and I need a "random" number between (inclusive) 0 and 15. I've been looking for something on the internet, and I can't find anything that I really understand.

The randomness of the sequence serves no integral purpose, it's only for aestetics (displaying 1 of 16 messages), but I would like it to appear to be random. The PIC C compiler should be able to handle a regular C algorithm, but I really have almost no experience with PICC. Usually I write in assembly on the PIC.

Thanks.

mmonnin
16 Jul 2005, 12:49am
http://www.cplusplus.com/ref/cstdlib/rand.html

printf ("A number between 0 and 99: %d\n", rand()%100);
printf ("A number between 20 and 29: %d\n", rand()%10+20);

So in your case rand()%16 since you want 0 to 15.

shwaip
16 Jul 2005, 1:41am
thanks - I thought I looked through all the libs that came with the PIC C compiler, but apparently I missed this one.

mmonnin
16 Jul 2005, 2:26am
We had a lab where we had to do some random numbers in one of my C++ courses.