Click Here!
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


The ANSI C standard does not define _lfind and _lsearch. If your code is to be ported to platforms other than Win32, you should consider judiciously the usage of nonstandard features in your programs. Remember also that the bsearch algorithm is more efficient because it uses a binary search to locate an element, whereas both _lsearch and _lfind use a linear search. _lsearch in particular is slower because it has to reallocate memory for the elements it appends to the array. For these reasons, you should prefer bsearch to _lfind or _lsearch whenever you have the choice.

8.5 Generate a sequence of random numbers?

Problem

I need to generate a sequence of random numbers in my program.

Technique

The Standard C Library provides two functions for the purpose of random numbers generation: rand and srand. This How-To uses these functions to generate a sequence of random numbers.

Steps

1.  Change to your base source directory and create a new subdirectory named RANDOM_DEMO.
2.  Start your source code editor and type the following code into a new file named MAIN.CPP:
#include <stdlib.h>
#include <stdio.h>

void main()
{
 int j = 0;

 for (; j < 5; j++)
  printf (“%d\n”, rand() );

}
3.  Save MAIN.CPP.
4.  Compile and link MAIN.CPP.
5.  Run the program; it will display five different numbers on your screen. Write down these numbers.
6.  Run the program once more; it should display the same five numbers that you wrote down.

How It Works

Let’s look at MAIN.CPP. First, it includes the header <stdlib.h>, in which the function rand is declared. It also includes the header <stdio.h> to use printf.

#include <stdlib.h>
#include <stdio.h>

The body of main() is very simple. A loop control variable, j, is defined and initialized. Next, a for loop executes five times. On each iteration, the loop displays the value returned from the function rand.rand generates a sequence of pseudorandom numbers.

void main()
{
 int j = 0;

 for (; j < 5; j++)
  printf (“%d\n”, rand() );

}

The term pseudorandom implies that the generated sequence is not really random; no matter how many times you execute the program, it generates exactly the same numbers. This is because rand has a set of values and a starting point. The default starting point is 1 and the generated sequence is between 1 and the macro RAND_MAX (defined in <stdlib.h> header). To create a unique sequence of numbers every time the program executes, you have to set a different starting point. You do that by using the function srand. To demonstrate that, add two changes to the source file MAIN.CPP:

1.  #include the standard <time.h> header.
2.  Add the following line immediately after the declaration of the variable j:
srand ( time(NULL) );

After these changes, MAIN.CPP should look like this:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void main()
{
 int j = 0;
 srand ( time(NULL) ); //set starting-point

 for (; j < 5; j++)
  printf (“%d\n”, rand() );
}

Now recompile and link MAIN.CPP. Run the program once and write down the generated numbers it displays. Run it once more. This time, a different sequence is generated.

The reason you get different sequences on every program run is that srand assigns a unique value to the starting point every time the program is executed. To create a unique value for srand, use the function time.time returns a long integer that holds the number of seconds elapsed since January 1, 1970.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.