Join us in Chat. Click link in menu bar to join. Unofficial chat day is every Friday night (US time).

0 Members and 1 Guest are viewing this topic.

// Random number seeduint8_t seed0=0xecu;uint8_t seed1=7;uint8_t seed2=0xcfu;/* Generate a random number from 0 to 255*/uint8_t random(void){ uint8_t tmp = (seed0 & 0x48) + 0x38; seed0<<=1; if(seed1 & 0x80){ seed0++; } seed1<<=1; if(seed2 & 0x80){ seed1++; } seed2<<=1; if(tmp & 0x40){ seed2++; } return seed0;}

uint8_t rnd(uint8_t minVal, uint8_t maxVal){ unit8_t range = maxVal - minVal + 1; return (random() % range) + minVal;}

#include <stdio.h>#include <stdlib.h>int GetRnd( int min , int max)// initalize the functionint GetRnd( int min , int max)// random number function{int MyRandomNumber;// the random numberint Seed;// the seed used to generate the random numberSeed = (PINB & 0b0001000);// read the value of pinb bit 3srand(Seed);// seed the random number generatorMyRandomNumber = rand() % max + min; generate the random number with in a given rangereturn MyRandomNumber;}

A random number generator actually returns the next number from a long sequence of 'random' numbers that takes a long time until it repeats the sequence. The 'seed' just gives the starting point in this long sequence. So you should only call it 'once' (say at the start of your 'main') - rather than every time you ask for the next number.

It can't give an infinite number of random values (if you request random numbers fast enough it will eventually run out of "entropy" and block until more entropy is acquired).