Sunday, May 10, 2009

How do i generate 16 bit random number in c?

You don't mention which compiler or operating system, but the ISO/POSIX standard function is rand() - there is a good write-up here: http://www.delorie.com/gnu/docs/glibc/li...





The return value is typically a 32 bit integer, but this can vary depending on the C library. You should be relatively safe casting a 32 bit return value into a 16 bit variable but, depending on your compiler, beware of sign extension issues.





A simple example:





short int my_rand;





srand (time(0));


my_rand = (short int)rand();





Note that rand() is really only a pseudo-random number generator, but it should be good enough for your purposes.





If you're on a Unix-like system, you have other options with the BSD and SystemV random number variants (referenced in the article above) but rand() is the most portable.

How do i generate 16 bit random number in c?
right click on your desk top click on properties go to settings and you can change it from there


No comments:

Post a Comment