LINUX TIPS AND TRICKS --- March 23, 2001

Published by ITworld.com -- changing the way you view IT
http://www.itworld.com/newsletters
_______________________________________________________________________

Generating Random Numbers
By Danny Kalev

This week, I will show how to generate random numbers. First, we will 
learn how to use the standard library's routines, and then we will 
focus on a Linux-specific technique.

ANSI C defines two functions, rand() and srand(), for generating random 
integral values. These functions are declared in the header file 
<stdlib.h> as follows:

    int rand();
    void srand(unsigned int seed);

Each invocation of rand() returns a pseudo-random number -- pseudo-
random because rand() produces the same sequence of numbers every time 
you run the program. To produce a different sequence on every 
execution, set a unique seed value by calling srand() before the first 
rand() call. It's customary to pass the current system time as an 
argument to srand(). So that forked processes or different instances of 
the same program that start in the same second generate unique random 
values, add the current process ID to the time argument:

    #include stdlib.h
    #include stdio.h
    #include time.h
    int main()
    {
     int n;
     srand(time(NULL) + getpid())
     for(n=0; n100; ++n) 
      printf(" %d ",rand());/*print 100 random numbers*/
    }

rand() guarantees that the generated numbers are relatively well 
distributed within the valid ranges. Thus, it can be used in game 
programming or protocols that insert random delays. Another advantage 
is its ability to reproduce a sequence (for example, when you debug a 
program) by providing the same seed value to srand().

Entropy Pool
Certain applications, encryption algorithms for instance, require non-
reproducible sequences of random values. Linux stores timings of 
external events such as mouse movements and key presses, and extracts 
information from these events. The results are stored in an entropy 
pool. If you need random numbers based on unpredictable events, you can 
read from one of two devices, namely /dev/random and /dev/urandom. 
The /dev/random device will return as many bytes as the device assumes 
exist in the entropy pool. The device /dev/urandom generates as many 
random numbers as you need, based on the current entropy pool. The 
values returned from these devices are the result of applying a hash 
algorithm to the entropy pool. You can find more documentation on the 
entropy pool, the random devices and their usage in the random driver 
source file drivers/char/random.c

About the author(s)
-------------------
Danny Kalev is a system analyst and software engineer with more than 10 
years of experience, specializing in C++ and object-oriented analysis 
and design on various platforms including VMS, DOS, Windows, Unix, and 
Linux. His technical interests involve code optimization, networking, 
and distributed computing. He is also a member of the ANSI C++ 
standardization committee and the author of ANSI/ISO C++ Professional 
Programmer's Handbook (Que, 1999). Contact him at linuxnl@excite.com.
_______________________________________________________________________

ADDITIONAL RESOURCES

OS identification 
The more a hacker knows about your system, the easier it is for him to 
get in 
http://www.unixinsider.com/jsw/lintps_nl/swol-12-2000/
swol-1208-buildingblocks.html

Are cryptography tools really only for crooks?
http://www.itworld.com/jump/lintps_nl/www.itworld.com/AppDev/1307/
IW010312opswatch/

Bell Labs cryptologist sees DSA flaw, fix
http://www.itworld.com/jump/lintps_nl/www.itworld.com/Sec/2170/
itwnws010205bellabs/

Security vendors converge at ComNet
http://www.itworld.com/jump/lintps_nl/www.itworld.com/AppDev/
1296/ITW0131com/
_______________________________________________________________________

COMMUNITY DISCUSSIONS

Linux Software Development
Hone your Linux development skills, share your expertise, and put out 
the occasional call for help in this discussion for programmers of all 
levels.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
webx?14@@.ee6b652/288!skip=221


Ask the Geek
Got a question about Linux setup, tuning, security, or maintenance? 
Hackers, newbies, and all the geeks in between knock heads and devise 
solutions in this discussion.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
webx?14@@.ee6c981/388!skip=337
______________________________________________________________________

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate Editor, 
  Newsletters at: andrew_santosusso@itworld.com

* For advertising information, write Dan Chupka, Account Executive at:
  dan_chupka@itworld.com

* For recruitment advertising information, write Jamie Swartz, Eastern
  Regional Sales Manager at: jamie_swartz@itworld.com or Paul Duthie,
  Western Regional Sales Manager at: paul_duthie@itworld.com

* For all other inquiries, write Jodie Naze, Product Manager, 
  Newsletters at: jodie_naze@itworld.com
_______________________________________________________________________

PRIVACY POLICY

http://www2.itworld.com/CDA/ITW_Privacy_Policy

Copyright 2001 ITworld.com, Inc., All Rights Reserved.
http://www.itworld.com
