tags:

views:

369

answers:

4

I am trying to code a random number generation function in embedded C where I can't include the math.h file. Thats why I'm not able to use the seed srand function.

Is there any other way to seed it other than time?

+3  A: 
  1. srand is in stdlib.h, not math.h. To seed with time you include time.h to use time(NULL), not math.h. I don't see how it's relevant.
  2. If rand and srand are not available, you can create one your own, e.g. LCG or Mersenne twister.
  3. If speed and security is not a concern, you can store the current PRNG state in persistent memory and restart from that point when a random number is required. For instance, MT19937 has period of 219937 - 1 which should be enough for normal purpose when without reseeding.
  4. If seeding is really required, anything that is not constant in any scales can be used as the seed.
KennyTM
oops sorry...i didnt mean math.h i meant to say time.h i hav n i can include stdlib.h but d prob is seed
Blossom
@blossum: Edit your question to clarify.
KennyTM
+3  A: 

Consider using the Mersenne Twister, sources are e.g. here -- much higher quality than traditional linear congruential generators, superbly long period, deeply studied in, and blessed by, plenty of recent academic literature.

Alex Martelli
But the problem was seeding the generator, and, supposedly, MT has to be seeded, too.
sbi
@sbi, sure, and `init_genrand` in the sources I pointed to does the seeding ("initializes `mt[N]` with a seed" is how the function's comment in the sources puts it), though you also have the richer `init_by_array` alternative there. My point is, Mersenne Twister is SO much better than linear congruential (the huge period reduces the need for seeding beyond "compilation time", if you can save and restore the computation's state, for example).
Alex Martelli
A: 

Consider having a looksee through this lecture, might give you some ideas (and code). The pdf goes through a few different options, and even gives a bit of code.

zdav
A: 

I am trying to code a random number generation function in embedded C where I can't include the math.h file. Thats why I'm not able to use the seed srand function.

srand() is commonly seeded using time(), and that is defined in <time.h>, not in <math.h>.

Is there any other way to seed it other than time?

Of course, you can seed it with anything you want. It depends on your platform what is available.

sbi
I am working on microcontroller 89c51...d only way i can see nw is external hw like noise generator or timer... do u know any software way?
Blossom
Hey, thank you all... I have done with that code :) I am new to this stackoverflow family...so please keep posting... and I will try too. :)Thanks once again.
Blossom
@blossum44: Since you're new here, you might want to spend a few minutes trying to learn the rules here. SO isn't just another forum, it works a bit different. One of these rules is that you should accept one of the answers as the one that helped you solving the problem - if there is such an answer. Oh, and I'd be quite interested in how you solved your problem.
sbi
Hi to all! I have solved the problem without srand() or any external hardware. I simply used the bit addressable property of 89C51 SFRs. And haan I have made some changes in header file of 89C51 in keil. If anybody wants the code I will paste it over here.@sbi hey, thanks for your help. As I am new here, I am finding it quite messy. I mean I cant see answers by one side and question by other. Can you help me with the settings please?
Blossom