Thread: Random number generator

  1. #1
    Registered User
    Join Date
    Feb 2011
    Location
    San Diego
    Posts
    10

    Random number generator

    so, for this assignment they introduce the rand() function and also(finally) other libraries to use. anyway, the first program is as follows:

    Write a program that will print out 10 random integers between 10 and 25. Prompt the user to enter the seed from which the random sequence will grow.

    i used an example out of my text, but naturally it didn't work. i also asked my t.a.'s about it and they didn't know how to do it either. this left me thrilled. anyway, i was wondering what anybody thought of this.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
       int x, i,z;
    
       printf("Enter a number between 10 & 25.\n");
       scanf("%d", x);
    
       printf("10 random numbers between values are as follows:\n");
    
       for(i=0; i<10; i++)
       {
          z=10 + (srand(x) % 25);
          printf("%d\n", z);
       }
       
       return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You only seed rand once. That is to say, you only call srand once. Then to get a number, you call rand.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Location
    San Diego
    Posts
    10
    cool, i got it. thanks.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       int seed, i,z;
       
       printf("RAND_MAX = %d\n", RAND_MAX);
    	
       printf("Enter seed:");
       scanf("%d", &seed);
    	 
       srand(seed);
    
       printf("10 random number between values are as follows:\n");
    
       for(i=0; i<10; i++)
       {
          z=10 + (rand() % 16);
          printf("%d\n", z);
       }
       
       return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Put srand up above the for loop.

    Add the ampersand '&' in front of x in the scanf() line of code.

    Since rand() % N, will give you 0 to N-1 in range, you need to add ten and mod '%' 25 in your rand() line of code.

    For example, if I wanted to get a range of 1 to 6, I'd use: rand() % 6 + 1. (rand % 6 by itself, would give me numbers ranging from 0 to 5).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  2. Good Random Number Generator
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 11-18-2004, 06:38 AM
  3. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  4. how to link random number generator with cpu?
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2003, 05:26 AM
  5. Seeding Random Number Generator
    By zdude in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2002, 03:10 PM