Thread: how do I generate a random number? (easily?)

  1. #1
    Registered User fsx's Avatar
    Join Date
    Apr 2009
    Posts
    29

    how do I generate a random number? (easily?)

    hello,

    I would like to generate a random number. I have looked on google but the way I found (using rand and srand from stdlib.h) doesn't works.

    I would like to generate a random number with two boundaries, say between 1 to 100.
    How do I do that? Still I am waiting to receive a book, so I am forced to look online...

    Thanks in advance.

    FSX

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    7
    i have found a solution for that before using the C compilers help menu..
    there are some example programs there using random function.. just try it..

    ^__^

  3. #3
    Registered User fsx's Avatar
    Join Date
    Apr 2009
    Posts
    29
    Quote Originally Posted by nhoda911 View Post
    i have found a solution for that before using the C compilers help menu..
    there are some example programs there using random function.. just try it..

    ^__^
    I have VC++ and the help only tells about classes. Otherwise I would have looked on it myself.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    The random() function returns a value between 0 and RAND_MAX. And you need to include stdlib to use that...

    you can use rand() function also...

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User fsx's Avatar
    Join Date
    Apr 2009
    Posts
    29
    Quote Originally Posted by matsp View Post
    Using random() it gives error. Using rand() only gives 41. I am eternally confused you're right! This is what I have done later, following a C++ program found on cprogramming:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
    	int random;
    	srand((unsigned)time(NULL));
    	random = rand()%100;
    
    	printf("random = %d", random);
    
    	return 0;
    }
    But it seems to depend on the seconds, so that the random number only changes after one second. Probably it is because of <time.h> which counts seconds. Is there a way to do that in milliseconds?
    Last edited by fsx; 05-12-2009 at 04:53 AM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, srand() will be set to a number corresponding to current time, and since that is in seconds, it's won't change more than once a second.

    However, if you want a SEQUENCE of numbers that vary a fair bit, then srand() will set the STARTING POINT, further calls to rand() will be picking different numbers [make a loop that produces 10 numbers in a row - and make sure you put the srand() call OUTSIDE the loop - your program should never call srand more than once, unless you SPECIFICALLY ARE looking to repeate the same sequence of random numbers - e.g. for simulation using different algorithms to compare the results of the same sequence of random numbers].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User fsx's Avatar
    Join Date
    Apr 2009
    Posts
    29
    Quote Originally Posted by matsp View Post
    Yes, srand() will be set to a number corresponding to current time, and since that is in seconds, it's won't change more than once a second.

    However, if you want a SEQUENCE of numbers that vary a fair bit, then srand() will set the STARTING POINT, further calls to rand() will be picking different numbers [make a loop that produces 10 numbers in a row - and make sure you put the srand() call OUTSIDE the loop - your program should never call srand more than once, unless you SPECIFICALLY ARE looking to repeate the same sequence of random numbers - e.g. for simulation using different algorithms to compare the results of the same sequence of random numbers].

    --
    Mats
    Well this is great, now it works! =) Thanks so much Matsp! =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  2. Generate Random Number
    By peacealida in forum C++ Programming
    Replies: 10
    Last Post: 04-06-2008, 08:57 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. random number in range
    By lgg in forum Linux Programming
    Replies: 3
    Last Post: 08-14-2005, 05:15 AM
  5. Replies: 2
    Last Post: 01-04-2004, 04:52 PM