Thread: Random number basics

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    132

    Random number basics

    Hi, everyone. I am trying to generate random numbers with the code below, but every time I run it, I get the number 41. What am I doing wrong? Thanks in advance!

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main (void)
    {
    
        int value;
    
        value = rand();
    
        printf("%d", value);
    
        return 0;
    
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You need to seed in the current time to the random number generator. Look up srand().
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Thanks!

  4. #4
    Registered User Alexander.'s Avatar
    Join Date
    May 2011
    Location
    Idaho
    Posts
    9
    The rand function should use an LCG and may always produce the same number sequences, hense the need of a seed which is fairly random (time in seconds) to cause the algorithm to produce different sequences for its period.

    If you require this for secure uses, or games, or where random numbers are important than the rand function is very unreliable.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stdq View Post
    Hi, everyone. I am trying to generate random numbers with the code below, but every time I run it, I get the number 41. What am I doing wrong? Thanks in advance!

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main (void)
    {
    
        int value;
    
        value = rand();
    
        printf("%d", value);
    
        return 0;
    
    }
    But the correct answer to life, love, the universe and everything else is ... 42...

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Alexander. View Post
    If you require this for secure uses, or games, or where random numbers are important than the rand function is very unreliable.
    Only if you don't know how to use it.

  7. #7
    Registered User Alexander.'s Avatar
    Join Date
    May 2011
    Location
    Idaho
    Posts
    9
    Quote Originally Posted by CommonTater View Post
    Only if you don't know how to use it.
    Edit: bad at forming words right now.

    LCGs are fast and can work for fairly equal distribution, although there are much better algorithms out there. Just the fact that every major compiler times platform has an LCG with such parameter diversity would just turn you off wanting to use it for anything predictably secure!
    Last edited by Alexander.; 05-14-2011 at 09:58 PM.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Alexander. View Post
    Edit: bad at forming words right now.

    LCGs are fast and can work for fairly equal distribution, although there are much better algorithms out there. Just the fact that every major compiler times platform has an LCG with such parameter diversity would just turn you off wanting to use it for anything predictably secure!
    The only issue with it not being that secure (unable to be predicted) is that the size of the internal state is too small and it isn't influenced by external factors such as the time the user last pressed a key or moved the mouse etc. Those issues can be overcome whilst still primarily using an LCG.

    Anyway that's all moot as the OP will possibly not be back seeing as they have an answer.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-05-2009, 10:21 AM
  2. Replies: 2
    Last Post: 12-25-2003, 01:31 AM
  3. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 AM
  4. random number
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-12-2002, 11:27 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM