Solved

How do I use rand() and srand()

Posted on 2000-03-14
3
2,233 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now