By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,834 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

functions

P: 7
how do we use the function randomize() to initialize randomization process?
Mar 14 '07 #1
Share this Question
Share on Google+
7 Replies


RedSon
Expert 5K+
P: 5,000
Is randomize even a proper method. I thought it was srand and rand.
Mar 14 '07 #2

100+
P: 1,646
how do we use the function randomize() to initialize randomization process?
this is from the help file in vc++
Expand|Select|Wrap|Line Numbers
  1. /* RAND.C: This program seeds the random-number generator
  2.  * with the time, then displays 10 random integers.
  3.  */
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <time.h>
  8.  
  9. int main( void )
  10. {
  11.    int i;
  12.  
  13.    /* Seed the random-number generator with current time so that
  14.     * the numbers will be different every time we run.
  15.     */
  16.    srand( (unsigned)time( NULL ) );
  17.  
  18.    /* Display 10 numbers. */
  19.    for( i = 0;   i < 10;i++ )
  20.       printf( "  %6d\n", rand() );
  21.    return 0;
  22. }
Mar 14 '07 #3

P: 7
Is randomize even a proper method. I thought it was srand and rand.
Ok, so how do we use srand?
Mar 14 '07 #4

P: 7
this is from the help file in vc++
Expand|Select|Wrap|Line Numbers
  1. /* RAND.C: This program seeds the random-number generator
  2.  * with the time, then displays 10 random integers.
  3.  */
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <time.h>
  8.  
  9. int main( void )
  10. {
  11.    int i;
  12.  
  13.    /* Seed the random-number generator with current time so that
  14.     * the numbers will be different every time we run.
  15.     */
  16.    srand( (unsigned)time( NULL ) );
  17.  
  18.    /* Display 10 numbers. */
  19.    for( i = 0;   i < 10;i++ )
  20.       printf( "  %6d\n", rand() );
  21.    return 0;
  22. }
Ok thanks, so why do you use function rand() in printf statement
Mar 14 '07 #5

100+
P: 1,646
Ok thanks, so why do you use function rand() in printf statement
It is returning a random number. In this case the number is printed to the screen just for demonstration purposes. With code snippets like this you should copy them to your ide and compile them to see how it works. That is how I learn this stuff :)
Mar 14 '07 #6

P: 7
It is returning a random number. In this case the number is printed to the screen just for demonstration purposes. With code snippets like this you should copy them to your ide and compile them to see how it works. That is how I learn this stuff :)
Ok thanks!
Mar 14 '07 #7

100+
P: 1,646
You are very welcome :)
Mar 14 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.