Random number with srand()

This is a discussion on Random number with srand() within the C++ Programming forums, part of the General Programming Boards category; I am working on a math tutor program that will generate two random numbers and display the result. I believe ...

  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
    671
    The source code is completely irrelevant to the question.
    Refer to Random seed - Wikipedia, the free encyclopedia and come back with more specific question.
    I never put signature, but I decided to make an exception.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    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().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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, 08: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, 04:16 PM

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