random integers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • malf
    New Member
    • Mar 2007
    • 1

    #1

    random integers

    it should generate integers from zero to 99, but it doesnt.
    Code:
    #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[]);
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    Originally posted by malf
    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:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define MAX 1000
    
    int
    main(void)
    {
    	int arr[MAX];
    	int i;
    	
    	srand((unsigned)time(NULL));
    
    	for (i=0; i<MAX; i++)
    		arr[i] = rand()%100;
    	for (i=0; i<MAX; i++)
    		printf ("%d\n",arr[i]);
    	
    	return 0;
    }
    Note that I changed the statement in the inner loop and the print out.

    Comment

    Working...