Thread: Is it possible to generate a certain number of random numbers in C?

  1. #1
    Registered User soawperL25's Avatar
    Join Date
    Dec 2013
    Location
    Middle-Earth
    Posts
    18

    Is it possible to generate a certain number of random numbers in C?

    I got a homework for a class where we're supossed to use Java. But since we just started the teacher let us to use C too if using Java is too difficult. I gave up with Java. So, I wrote in C instead. I know in Java it's possible to generate a number of random numbers, but how about in C? My program is almost complete. The only problem is I don't know how to generate random numbers.

    the program is supposed to generate a number of random numbers. So, do I need to use loop like this?

    Code:
    for(x=0; x<VALUE; x++)
        {
            srand ( time(NULL) );
            arr[x] = rand() % 10 + 1;
            printf("--> %d\n", arr[x]);
        }
    Let's say VALUE is 10. So, the program should generate 10 different numbers. But, that wasn't what i got.
    Instead, I got something like this.

    --> 10
    --> 10
    --> 10
    so on...

    or
    --> 5
    --> 5
    --> 5
    so on..

    It generates the same numbers! I really do not know way. it is the first time I use srand() and rand(). So, please someone tell me the mistake.

    One last thing, how do I generate negative numbers?

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    you only need to call srand() once

    Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand.
    Last edited by africanwizz; 03-11-2014 at 05:50 AM. Reason: meh..

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by soawperL25 View Post
    It generates the same numbers! I really do not know way. it is the first time I use srand() and rand(). So, please someone tell me the mistake.
    Call srand() once, and only once, before the loop.

    Quote Originally Posted by soawperL25 View Post
    One last thing, how do I generate negative numbers?
    If you multiply positive values by -1, the result will be negative.

    Subtracting 10 from a set of values between 0 and 25 will produce results between -10 and 15.

    In general, rand() is specified to produce a value between 0 and RAND_MAX. If you have some other requirement, work out some transformation that, for every value between 0 and RAND_MAX, produces a value that meets your needs.

    Given your difficulties doing this in C (it is actually quite simple) the mind boggles at what you consider is difficult about doing it in Java.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User soawperL25's Avatar
    Join Date
    Dec 2013
    Location
    Middle-Earth
    Posts
    18
    Wow, thank you very much! That really helps. So, I finally changed it into this
    Code:
    srand ( time(NULL) );
        for(x=0; x<VALUE; x++)
        {
            arr[x] = rand() % 20 * -1 + 10;
            printf("%d\t", arr[x]);
        }
    Thanks again, everyone!

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,750
    Quote Originally Posted by soawperL25 View Post
    Wow, thank you very much! That really helps. So, I finally changed it into this
    Code:
    srand ( time(NULL) );
        for(x=0; x<VALUE; x++)
        {
            arr[x] = rand() % 20 * -1 + 10;
            printf("%d\t", arr[x]);
        }
    Thanks again, everyone!
    I am giving you an additional exercise:

    * Explain why moving the srand() outside of the loop changed what is printed; i.e. why did doing this fix the code?

  6. #6
    Registered User soawperL25's Avatar
    Join Date
    Dec 2013
    Location
    Middle-Earth
    Posts
    18
    Quote Originally Posted by Hodor View Post
    I am giving you an additional exercise:

    * Explain why moving the srand() outside of the loop changed what is printed; i.e. why did doing this fix the code?
    I guess that is because srand() is the function which generates the time for random the number. So, when I moved it out it generates different number for each loop? Lol, no. Can't really figure it out. So, what is it?

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    3,863
    Read srand - C++ Reference

    Hint: Your program was running real fast compare to the units used in the time function.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to generate a random number in c?
    By blogchama in forum C Programming
    Replies: 2
    Last Post: 01-20-2011, 10:39 AM
  2. Generate Random Number
    By peacealida in forum C++ Programming
    Replies: 10
    Last Post: 04-06-2008, 08:57 AM
  3. How to generate random numbers between 65 and 75
    By chottachatri in forum C Programming
    Replies: 19
    Last Post: 03-02-2008, 06:24 PM
  4. generate a random number
    By waxydock in forum C++ Programming
    Replies: 5
    Last Post: 06-05-2005, 07:43 PM
  5. Ask about generate Random number
    By ooosawaddee3 in forum C Programming
    Replies: 2
    Last Post: 07-01-2002, 04:30 AM

Tags for this Thread