I have to generate a random number by taking the input as a timer value... SO can anyone suggest how to get this????
Thanks in Advance
Did you spend some time with Google?
Did you get any ideas from all the links Google can supply?
Where did you get stuck? What do you want clarified?
This sounds like homework. If somebody does it for you, what's the point in doing it?
yes, All are using the random function to generate random number... In embedded C can we use this rand function???
I didn't ask to you to do it.. I asked the help how to start with.....
But you showed no indication whatsoever that you'd dony any thinking or research of your own - so what were people supposed to think?
"I asked the help how to start with"
No, you actually asked how to "get this".
But you already have your start - you said, "by taking the input as a timer value"
What thought have you given to that?
How would the timer help?
Does the standard 'C' rand function give a truly random number?
Have you searched this forum for "random" ?
No normal implementation of rand() will generate any random number.
rand() generates a pseudo-random number.
Good enough for some tasks, but much too lousy for lots of problems.
But besides rand() you also needs to start this virtual number sequence with a seed. This can be done with srand(). But what randomness do you have access to in your product, that will make sure that if you power up your unit 100 times, you get 100 different seed values?
Equipment connected to a serial terminal may use the pause between received characters to seed the rand() generator. Same if device have ethernet.
But in the end, _you_ must figure out how you can get your device to produce different sequences every time you start it. And it isn't trivial.
A hint ... don't bother with the one I saw during a code review recently:
. . srand(rand()); . .
srand(rand());
That was a real beauty.
With such code, the guilty part should buy a nice cake or something.
Sorry for my very limited English and technical ability, I guess this
is a really bad idea, am I right?
The srand() function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand(). If srand() is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If rand() is called before any calls to srand() are made, the same sequence shall be generated as when srand() is first called with a seed value of 1.
I noticed that it is 1-Apr-2011 now. But just not so sure.
"...a really bad idea, am I right?"
Yes, you are right.
No, it is nothing to do with April 1st. I really did see that line of code!
The line of code was being executed close to the start of main, so the seed for the pseudo-random number generator had been set by the initialisation of the library. The first random number produced was identical from one reset to another. So the value passed to srand was always the same. So subsequent calls to rand always produced the same sequence of values.
Best is if srand() can be called after the program have been able to pick up some non-constant information that is different each boot.
Not so good is to use the time from the RTC (which, in case the RTC is battery-operated, would differ between each boot) since it is predictable. But since rand() itself can't produce cryptographic-strength random values, RTC data might be good enough.
Better is if there is something that will vary each boot and also can't be predicted. The problem with timers is that they run with a fixed frequency, so they are predictable. To get good use of a timer, it must run with a very high frequency and the time when it is sampled must be unpredictable. A program that always runs the same instructions between the initialization of the timer and the read-out of a timer value, will get the same values - or possibly a very small number of possible values because the CPU core may perform an asynchronous read of the timer.
An ADC might produce a _small_ amount of noise on the read data, that could, after enough reads produce enough random bits. Any interaction with a human being would produce random bits from the varying delays the human introduces between every interaction.
A "good" random source would have been the capturing of thermal noise, which would not only have given a good source of data to use for an initial call to srand(), but would allow the srand()/rand() functions to be completely ignored completely. After all, rand() just creates a limited-length series of pseudo-random numbers, where srand() controlls the start position of this series.
No reply? I'll take that as a "no", then?
www.keil.com/.../search.asp
www.google.com/search
> But you already have your start - you said, "by taking the input as a timer value" > > What thought have you given to that? > > How would the timer help?
I think you might have misunderstood the OP. He doesn't want to use the timer to generate the random number, but rather the other way 'round. I believe that the random number is supposed to serve as start value for a timer. So it should rather read: "by taking the output as a timer value".
@Per Nobody said anything about cryptographic strength. We don't know what exactly the PRN is going to be used for. There are many useful applications even for perfectly predictable PRNs.
Kindly, Marcus http://www.doulos.com/arm/
The initial post was: "I have to generate a random number by taking the input as a timer value..."
With your change, that becomes:
"I have to generate a random number by taking the output as a timer value..."
which doesn't make sense.
Only the OP can clarify this.
Well, the OP seems to have lost interest, but here's an interesting idea on how to get a truly random seed for your pseudo-random number generator (PRNG):
www.eetimes.com/.../True-random-number-generator-IP-for-ASICs-and-FPGAs
(it is applicable to microcontrollers - not just ASICs and FPGAs)