rand() gives only one number...

This is a discussion on rand() gives only one number... within the C++ Programming forums, part of the General Programming Boards category; Hey. Im a new C++ programmer and having trouble using rand() in programs. the only number rand returns is 41. ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    4

    Question rand() gives only one number...

    Hey.
    Im a new C++ programmer and having trouble using rand() in programs.
    the only number rand returns is 41.
    Is there anything I can do to make it generate other numbers?
    Or is there any better generators?
    Thank you for reading this thread and have a good day =).

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    How are you using it? Are you calling srand() first?
    Post an example.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    >> Is there anything I can do to make it generate other numbers?

    Of course not. Why would you want any other number? How useful would that be?

    >> Or is there any better generators?

    The implementation of rand is not dictated by the standard, so really just depends on how it is implemented on your platform.

    >> Im a new C++ programmer and having trouble using rand() in programs.

    You need to seed your generator before you first use it. Look up srand.



    ITSA
    Socket Library!

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    Okay i have looked srand up.
    But i still don't understand its syntax :S
    srand(time(NULL))

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    That has nothing to do with it's syntax, but one particular usage. You should probably get a hold of some decent CRT API documentation. Here's what mine says:

    Header File

    stdlib.h

    Category

    Math Routines

    Syntax

    #include <stdlib.h>
    void srand(unsigned seed);

    Description

    Initializes random number generator.

    The random number generator is reinitialized by calling srand with an argument value of 1. It can be set to a new starting point by calling srand with a given seed number.

    Return Value

    None.
    Exercise: Now look up the documentation for the 'time' function. Things will be much clearer then.



    ITSA
    Socket Library!

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Remember that the seed must be initialized with a unique number, because a randomizer is basically just an algorithm applied to a unique number.
    This is why we see this particular line of code very often.
    Once you read the documentation on time, it should be clear.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    okay. can you give me a example of rand() where it gives a totally random number and im not sure
    what time has to do with random numbers.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Code:
    int main()
    {
        std::srand( (unsigned int)std::time(NULL) );
        for (int i = 0; i < 10; i++)
            std::cout << std::rand() << std::endl;
    }
    It's still your job to figure out how this works.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    >> im not sure what time has to do with random numbers.

    Random number generators are generally not really random, but fully deterministic and thus "psuedo-random". That simply means that if you give a particular implementation the value 3114 it will generate the same sequence of numbers everytime. Naturally, you'll probably want to get different values in most cases, so you need a function that can produce an ever-changing flow of numbers, and the 'time' function is one such source. But there are other possibilities, of course. You could access the CPU temperature, for instance, and produce a seed from that.



    ITSA
    Socket Library!

  10. #10
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    Ahh i get it now, Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 12:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

Tags for this Thread


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