Altera_Forum
Honored Contributor
21 years ago"Random" Instruction
Hi,
can anyone tell me how to generate random numbers in the NIOS II , for the IDE software in C ? http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/huh.gif thanksHi,
can anyone tell me how to generate random numbers in the NIOS II , for the IDE software in C ? http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/huh.gif thanksAre you asking about a hardware, or a software random number generator?
--Scottoh sorry, a software generated instruction for NiosII.
Hi Toysoldier,
You could devote an entire career to this subject ... regardless, here are a few resources I've used in the past that may be helpful: http://csrc.nist.gov/rng (http://csrc.nist.gov/rng) -- everything you wanted to know and more ;-) http://www.embedded.com/showarticle.jhtml?...icleid=20900500 (http://www.embedded.com/showarticle.jhtml?articleid=20900500) -- nice article with some C code http://www.opencores.org/projects.cgi/web/...ng_lib/overview (http://www.opencores.org/projects.cgi/web/rng_lib/overview) -- VHDL code Hope this is helpful. Regards, --Scottahh.... thanks scott...
i thought NIOS would have a built-in code like MATLAB's 'randn'. seems like i am wrong. http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/wink.gifI haven't used it myself, but rand() is provided as a part of the C library. You'll find the details in the newlib documentation.
A good way to get a random number is to use the one out of the C library like monkeyboy said, and seed it with an external hardware counter that you let count away independently (avalon slave that you can read from). The seed value helps randomize the rand() call so that you don't get the same "random" sequence each time. Having an external counter isn't going to be perfect but it will give you a much more random sequence since the likelyhood of the value producing the same seed all the time is going to be really rare.
It all depends what you need the random number for. rand() is good enough for a lot of things, but it generates a fixed sequence of numbers which won't be appropriate for some applications.
If you want a real random number then you need to drive it from some sort of non-deterministic process as the earlier links suggested.