This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
I want to generate Random number between 0 to 4 and want to store that in 1 variable which i am initializing to 0 right now. I want to put that random number while programming. so that every micro will have different number stored in that variable. how i can do that.
I do not quite understand what you want. Some others my put a serial number in their micro so that every one will have a different number. A lot of production programmers even have the feature to serialize the micros automatically.
But you said you want that number to be "Random". Why?
What puzzles me more is that your Random number is between 0 to 4. That means if you make more than 5 units, you will always have micros with identical "Random number" amongst them. Even if you only make 2 units, you will have 20% chance that these 2 units have the same "Random number".
abhishek,
Which family are you looking at using? the biggest issue most people have with random number generators(RNGs), is the starting seed. If you put the same start seed in each of the microcontrollers, you will get the same set of no so random results.
Tricks I have done in the past include:
Reading a floating ADC pin.
Reading analog/dco calibration data.
Having a free running timer until an event occurs, and use that timer as the seed.
Also, take a look at this other thread and it will give you some additional insight into RNG.
http://e2e.ti.com/support/microcontrollers/msp43016-bit_ultra-low_power_mcus/f/166/t/32736.aspx
Hope this helps!
-Jason
The Elprotronic programming software has a feature to put serial numbers into the code during flash. You can define the memory location of the number (or text) and provide a file from which the values are fetched one by one (counter is saved between programming sessions).
The list you can provide e.g. with Excel.
Newer MSPs have a TLV table in which a unique number is stored (wafer Nr and x/y position on wafer). You can extract a device serial number from this at runtime without any difference in the code itself.
old_cow_yellow,
I have to have diffrent number of days for each unit. the program works like adding number of days and writing that number into flash. for eg. if micro's flash location initialized with '0' every day it increments. i want that '0' to be random for each unit between 0 to 51. that means each unit will have diffrent initialization point for eg. 0 .4 or any number.
How i can genrate that numbr and store that in flash
Sorry, I do not know how to help you. You keep asking for random numbers yet you want a different number for each unit. Random numbers can equal to each other randomly -- especially when the range is small such as 0 to 4 or 0 to 51. Unless you are only going to make 1 unit. In that case that random number will be different for each unit.
Sorry, I do not know how to help you. You keep asking for random numbers yet you want a different number for each unit. Random numbers can equal to each other randomly -- especially when the range is small such as 0 to 4 or 0 to 51. Unless you are only going to make 1 unit. In that case that random number will be different for each unit.
Just for posterity: if you have limited scope (small range of values), you are not too concerned about the quality (it's not going into cryptography), and you have the FLL enabled, you can try this hack to return an 8bit pseudo random number.
prand_8 = (u8)(UCSCTL0 >> 3);
I've found that there is not always enough variance in the upper bits, but the lower bits (3:0) seem to change pretty quickly. If you want to take it a step further, use the 4bit pseudo rand value to rotate an LSFB. Here's an example of one that uses the version of PN9 incidentally used but most TI chipcon radios.
u16 PN9reg; void init_PN9() { PN9reg = 0x01FF; } u8 get_PN9() { return (u8)(PN9 & 0xFF); } void rotate_PN9() { /// nibble-wise PN9 implementation. Runs fast, no table. u16 x; x = (PN9reg << 5) ^ PN9reg; x &= 0x01E0; PN9reg >>= 4; PN9reg |= x; x = (PN9reg << 5) ^ PN9reg; x &= 0x01E0; PN9reg >>= 4; PN9reg |= x; } u8 prand_u8() { u8 index = (u8)(UCSCTL0 >> 3) & 0xF; while (index-- != 0) { rotate_PN9(); } return get_PN9(); }
**Attention** This is a public forum