NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Random Number Generator

July 23, 2012
by lnino
lnino's Avatar

Hi at all,

i have studied several threads in this and other forums about this topic, but I was not able to figure it out.

All I want is a generator which gives me 4 different number between 1 and 4 everytime the loop runs.

How did I get that work? I have tried it like that, but I am only getting the same numbers again.

// for NerdKits with ATmega168

#define F_CPU 14745600

#include <avr/io.h>
#include <inttypes.h>
#include <util/delay.h>
#include <stdio.h>
#include <stdlib.h>

#include "../libnerdkits/lcd.h"

int main(void) {

    char test[4];

    while(1) {

        int i;

        for(i = 0; i < 4; i++){
  return 0;

Solving this in Visual Studio is easy, because I can use srand(time(0)) to generate nearly random numbers. But on the MCU it's much more difficult.

Maybe someone can help me to solve this.

July 23, 2012
by Rick_S
Rick_S's Avatar

Often srand is seeded using a timer. Sometimes a timer and a button press as in a menu might say press a button to continue and when the button is pressed, the current value of the timer is read and the random seed generated from that. That would eliminate the repetition you get from using the same seed number each time. Another method is to read a value from one of the ADC's onboard. This can be done with nothing attached to the ADC and the "noise" on the ADC channel can produce a good random seed.

Hope I gave you some ideas to think about.


July 24, 2012
by lnino
lnino's Avatar

Hi Rick,

i have read about the both possibilities you meantioned. But I wasn't able to Figure Out how to use them.

Have you used One of this types in your Projects. Maybe you have an example Code.

Thanks for helping.

July 24, 2012
by Rick_S
Rick_S's Avatar

An easy one would be using the "Noise" in the ADC (Analog to Digital Converter). You wouldn't need to use rand or srand at all.

Take the temperature sensor program. Set up a variable for your random number.

uint_t rnum=0;

Then, since you want a number between 1 and 4, this is only two bits. If you take two samples from the ADC, and take the least significant bit from each, shift one of them and put them into your number you'll have a pretty random number between zero and 3 add 1 and it's between 1 and 4. Now keep in mind, random means you may get the same number twice in a row.

To do this you could do something like:

rnum = 1 & adc_read();
rnum |= (1 & adc_read()<<1);
rnum ++;

Now this is untested code, and my mind may be a bit rusty, but I think this would make the least significant bit of rnum 1 if the LSB of the first ADC read was 1, and the 2nd bit of rnum a 1 if the LSB of the second ADC read was 1. Thus creating a psudo random number between 0 and 3 increment and now it's between 1 and 4.

I don't guarantee the code works as written, but it should point you in a direction.


July 25, 2012
by lnino
lnino's Avatar

Hi Rick,

Thanks for your Reply.

I am sure your mind is Not rusty.

Thanks for giving an example with Great Explanation. I will try it when i am back home.

greetings Nino

Post a Reply

Please log in to post a reply.

Did you know that a piezoelectric buzzer can be used in reverse as a microphone? Learn more...