Solved

How do I use rand() and srand()

Posted on 2000-03-14
3
2,245 Views
Last Modified: 2008-03-10
I have read about them but do not really understand how to implement them. Really all I want is an example (annotated) in using them to generate random numbers. For example print out a new 4 digit random number each time a program loops. I know the syntax:

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

how to I actually use these in a program?

Cheers

Jim
0
Comment
Question by:Jim2000
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
Wyn earned 75 total points
ID: 2615756
Here from MS:
/* RAND.C: This program seeds the random-number generator
 * with the time, then displays 10 random integers.
 */

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

void main( void )
{
   int i;

   /* Seed the random-number generator with current time so that
    * the numbers will be different every time we run.
    */
   srand( (unsigned)time( NULL ) );

   /* Display 10 numbers. */
   for( i = 0;   i < 10;i++ )
      printf( "  %6d\n", rand() );
}


Output

    6929
    8026
   21987
   30734
   20587
    6699
   22034
   25051
    7988
   10104

=======================
Free to ask for furthur explanation

Regards
W.Yinan
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2615767
->. For example print out a new 4 digit random number each time a program loops.
=================================
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void main( void )
{
   int i;
   int PROGRAMLOOP
   /* Seed the random-number generator with current time so that
    * the numbers will be different every time we run.
    */
   srand( (unsigned)time( NULL ) );

  for( int j=0;j<PROGRAM_LOOP;j++)
  for( i = 0;i <4;i++ )
      printf( "%6d\n", rand() );
}
===========================

Jim2000 , plz note that srand() should be used only one time before sequentially rand() calls.

Regards
W.Yinan
0
 

Author Comment

by:Jim2000
ID: 2616238
Cheers perfect
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question