Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

"Random" Instruction

Altera_Forum
Honored Contributor II
1,648 Views

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  

 

thanks
0 Kudos
7 Replies
Altera_Forum
Honored Contributor II
933 Views

Are you asking about a hardware, or a software random number generator? 

 

--Scott
0 Kudos
Altera_Forum
Honored Contributor II
933 Views

oh sorry, a software generated instruction for NiosII.

0 Kudos
Altera_Forum
Honored Contributor II
933 Views

Hi Toysoldier, 

 

You could devote an entire career to this subject ... regardless, here 

are a few resources I&#39;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, 

--Scott
0 Kudos
Altera_Forum
Honored Contributor II
933 Views

ahh.... thanks scott... 

 

i thought NIOS would have a built-in code like MATLAB&#39;s &#39;randn&#39;. 

 

seems like i am wrong. http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/wink.gif
0 Kudos
Altera_Forum
Honored Contributor II
933 Views

I haven&#39;t used it myself, but rand() is provided as a part of the C library. You&#39;ll find the details in the newlib documentation.

0 Kudos
Altera_Forum
Honored Contributor II
933 Views

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&#39;t get the same "random" sequence each time. Having an external counter isn&#39;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.

0 Kudos
Altera_Forum
Honored Contributor II
933 Views

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&#39;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.
0 Kudos
Reply