Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to generate random numbers in "C" with AVR Studio?

Status
Not open for further replies.

avinash.kashyap

Junior Member level 3
Joined
Jan 11, 2011
Messages
30
Helped
8
Reputation
16
Reaction score
8
Trophy points
1,298
Activity points
1,642
...hi, i'm using AVR Studio/ GCC + WinAVR...and using function as n=rand()*6, trying to generate any random number between 0 to 5 but the problem is everytime my MCU gives me the same sequence of "random" numbers e.g. {6, 5, 3, 1, 4, 2 ...and it repeats}...so can someone help to generate real unpredictible random numbers?...
 

You can use this random number generator function,
I usually pass a value from a timer to it




Code C - [expand]
1
2
3
4
5
6
7
#define M 0x7FFFFFFF  // 2^31-1, the modulus used by the psuedo-random
 
long int prng(long int x) {  //random number generator; call with 1 <= x <=M-1
 x = (x >> 16) + ((x << 15) & M)  - (x >> 21) - ((x << 10) & M);
 if (x < 0) x += M;
 return x;
}



Alex
 

The seed I already used was the A/D value due uC was measuring the main line voltage.
The reason is that it is really assincronous with uC timing.

+++
 

my idea....use timers....initiate one....take a number out...would be random every time u take it out.....timer running with 12mhz mips would change value every ((1/12)*10^-12) seconds!....no body is that quick
 

uxmanz,

If I understood correctly, what you posted don´t work.
Note that the built-in uC timer is colcked by same source than core.

It is necessary an external assincronous event to feed input of random funcion.
I performed something like what you said and generated everytime the same value.

+++
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top