Solved

How do I use rand() and srand()

Posted on 2000-03-14
3
2,252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to understand recursion 12 236
c++ getting the first 10 characters of a char* string 11 108
C qsort compare function issue 6 68
Reactjs with .NET 3 117
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

738 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