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

random integers

P: 1
it should generate integers from zero to 99, but it doesnt.
Expand|Select|Wrap|Line Numbers
  1. #include<stdlib.h>
  2.  
  3. int main(void)
  4. {
  5.    int arr[MAX];
  6.    int i,n;
  7.    for (i=0; i<MIN_A; i++)
  8.       srand((unsigned)time(NULL));
  9.    printf ("%d",arr[]);
Mar 17 '07 #1
Share this Question
Share on Google+
1 Reply


arne
Expert 100+
P: 315
it should generate integers from zero to 99, but it doesnt.
#include<stdlib.h>

int
main(void)

{
int arr[MAX];
int i,n;
for (i=0; i<MIN_A; i++)
srand((unsigned)time(NULL));
printf ("%d",arr[]);
'srand' only sets the seed for the random number generator, i.e. it initializes the generator. 'rand' is the function you want to call afterwards in order to get a random number. Try this code:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define MAX 1000
  6.  
  7. int
  8. main(void)
  9. {
  10.     int arr[MAX];
  11.     int i;
  12.  
  13.     srand((unsigned)time(NULL));
  14.  
  15.     for (i=0; i<MAX; i++)
  16.         arr[i] = rand()%100;
  17.     for (i=0; i<MAX; i++)
  18.         printf ("%d\n",arr[i]);
  19.  
  20.     return 0;
  21. }
  22.  
Note that I changed the statement in the inner loop and the print out.
Mar 17 '07 #2

Post your reply

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