random integers

This is a discussion on random integers within the C Programming forums, part of the General Programming Boards category; hi this is my first time to visit this site hope some1 can help me how to generate a random ...

  1. #1
    Unregistered
    Guest

    random integers

    hi this is my first time to visit this site hope some1 can help me

    how to generate a random integers in a given range say from is nto a problem for me but the problem is who to get more than one integer (ie the code i used only gives me one integer how can i get n number of integers )

    this is my code

    int range,num,r,i ;

    {
    printf("Enter the range size(max=20)");
    scanf("%d",&range);
    printf("How many numbers do you wish to generate?");
    scanf("%d",&num);

    date_time_seed();
    r=1+rand()%range;


    }


    printf("%d\n",r);

    return(0);
    }


    i think it is to do with for loop but i dunno who to do it using the input num

    can any1 help plz

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try a for-loop.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
       int range,num,r,i ; 
    
       printf("Enter the range size(max=20)"); 
       scanf("%d",&range); 
       printf("How many numbers do you wish to generate?"); 
       scanf("%d",&num); 
    
       srand((unsigned) time(NULL));
       for (i=0; i<num; i++)
       {
          r=1+rand()%range; 
          printf("%d\n",r);
       } 
    
       return(0); 
    }

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    16
    The way I have always used is:

    srand ( time(0) );
    var = (int) (rand() / (RAND_MAX + 1.0) * (high - low + 1);

    Example:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
     int randnum, high, low, num, i;
     
     srand( time(0) );
     
     printf("Please enter the range (example 1 - 20): ");
     scanf("%d - %d",&low,&high);
    
     printf("\nHow many number do you wish to get?: ");
     scanf("%d",&num);
    
     for (i = 0;i < num;i++)
         {
          randnum = (int) (rand() / (RAND_MAX + 1.0) * (high - low + 1) + low);
          printf("\n%d",randnum);
         }
     return 0;
    }
    pr0ficient@hotmail.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making a random array of integers
    By Vidak in forum C# Programming
    Replies: 2
    Last Post: 11-09-2007, 05:00 AM
  2. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM
  3. Generating 1000 random integers
    By nizbit in forum C Programming
    Replies: 7
    Last Post: 12-09-2004, 05:40 PM
  4. arrays of random integers & time
    By meeloff in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2004, 03:35 PM
  5. Writing a set of random integers to a file
    By RazielX in forum C Programming
    Replies: 19
    Last Post: 09-17-2004, 07:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21