[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How do I use rand() and srand()

Posted on 2000-03-14
3
Medium Priority
?
2,260 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 300 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

650 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