Thread: Random number with srand()

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    5

    Random number with srand()

    I am working on a math tutor program that will generate two random numbers and display the result. I believe I have to use the srand() function to generate random number. The book mentions a seed value which I am a very unclear on. Could I get an explanation on what a seed value is? Below is the source that I have got so far.

    Code:
    #include<iostream>
    #include<cstdlib>
    #include<cmath>
    using namespace std;
    int main()
    {
    	int x,y,z;
    	char(ch);
    	x=rand();
    	y=rand();
    
    	z=x+y;
    	
    	
    	cout<<"What is the sum of: "<<x<<" and "<<y<<endl;
    
    	cout<<"Press a key of the answer!"<<endl;
    	cin.get(ch);
    	
    	cout<<"The sum of: "<<x<<" and "<<y<<" is "<<z<<endl;
    
    
    
    
    	system("pause");
    
    	return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    The source code is completely irrelevant to the question.
    Refer to Random seed - Wikipedia, the free encyclopedia and come back with more specific question.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The seed value determines the initial state of the pseudorandom number generator (PRNG), which in turn determines which sequence of pseudorandom numbers is generated. Thus, you use srand() to seed and rand() to obtain the next number in the sequence. In your example, you do not use srand() at all, so the PRNG is seeded as if you wrote srand(1).

    Also, read Prelude's article on using rand().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The easiest way of initializing the random number generator is basically

    std::srand(std::time(nullptr));

    Don't forget to include appropriate headers.
    That said, it's still a good idea to read Prelude's article on rand.
    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.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    5

    Time as a seed

    Thanks for the feedback..

    Using the time function for my seed value.

    srand(time(0));

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Use nullptr, not 0.
    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
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by Elysia View Post
    The easiest way of initializing the random number generator is basically

    std::srand(std::time(nullptr));

    Don't forget to include appropriate headers.
    That said, it's still a good idea to read Prelude's article on rand.
    On a totally different topic, is she still writing those articles anymore?
    "All that we see or seem
    Is but a dream within a dream." - Poe

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Dunno. How about you ask her?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple XOR Program
    By dolfaniss in forum C Programming
    Replies: 8
    Last Post: 05-24-2010, 01:27 PM
  2. random number generator help
    By mayoussa89 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2010, 07:26 AM
  3. couple questions for very small random number program
    By nick753 in forum C++ Programming
    Replies: 7
    Last Post: 01-17-2010, 09:35 PM
  4. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  5. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM