Thread: Need Help Generating Random Numbers In C++

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    21

    Need Help Generating Random Numbers In C++

    Okay dudes, I have this program and when I run it I need it to generater random numbers, but I can't seem to figure out how to use the srand() thing to do it. Thank for the help in advance.

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
    	
    	bool TrueFalse1, TrueFalse2, TrueFalse3, TrueFalse4, TrueFalse5;
    	long int xNumber = 1 + rand() % 50,
    		yNumber = 1 + rand() % 50,
    		A = 1 + rand() % 50,
    		B = 1 + rand() % 50,
    		C = 1 + rand() % 50,
    		result1, result2, result3, result4, result5,
    		answer1, answer2, answer3,answer4,answer5,
    		result,
    		total,
    		x = xNumber,
    		y = yNumber;
    
    	cout<<"Solve the following expression where x = "<<xNumber << " and y = "<<yNumber<<endl;
    	cout<<A<<" x + "<<B<<" y + "<<C<<endl;
    	cout<<"Write your result:\n";
    	cin>> result1; 
    	answer1 = (A * x) + (B * y) + C;
    	
    	
    	if (result1 == answer1)
    		cout<<"Your answer is correct\n";
    	else
    		cout<<"Your answer is wrong. The correct answer is "<<answer1<<endl;
    	TrueFalse1 = (result1 == answer1);
    
    
    
    	
    	cout<<"Solve the following expression where x = "<<xNumber<<" and y = "<<yNumber<<endl;
    	cout<<A<<" x + "<<B<<" y + "<<C<<endl;
    	cout<<"Write your result:\n";
    	cin>> result2;
    	answer2 = (A * x) + (B * y) + C;
    	
    
    	if (result2 == answer2)
    		cout<<"Your answer is correct\n";
    	else
    		cout<<"Your answer is wrong. The correct answer is "<<answer2<<endl;
    	TrueFalse2 = (result2 == answer2);
    
    	
    	cout<<"Solve the following expression where x = "<<xNumber<<" and y = "<<yNumber<<endl;
    	cout<<A<<" x + "<<B<<" y + "<<C<<endl;
    	cout<<"Write your result:\n";
    	cin>> result3;
    	answer3 = (A * x) + (B * y) + C;
    	
    
    	if (result3 == answer3)
    		cout<<"Your answer is correct\n";
    	else
    		cout<<"Your answer is wrong. The correct answer is "<<answer3<<endl;
    	TrueFalse3 = (result3 == answer3);
    
    	
    	cout<<"Solve the following expression where x = "<<xNumber<<" and y = "<<yNumber<<endl;
    	cout<<A<<" x + "<<B<<" y + "<<C<<endl;
    	cout<<"Write your result:\n";
    	cin>> result4;
    	answer4 = (A * x) + (B * y) + C;
    
    
    	if (result4 == answer4)
    		cout<<"Your answer is correct\n";
    	else
    		cout<<"Your answer is wrong. The correct answer is "<<answer4<<endl;
    	TrueFalse4 = (result4 == answer4);
    	
    
    	
    	cout<<"Solve the following expression where x = "<<xNumber<<" and y = "<<yNumber<<endl;
    	cout<<A<<" x + "<<B<<" y + "<<C<<endl;
    	cout<<"Write your result:\n";
    	cin>> result5;
    	answer5 = (A * x) + (B * y) + C;
    
    
    	if (result5 == answer5)
    		cout<<"Your answer is correct\n";
    	else
    		cout<<"Your answer is wrong. The correct answer is "<<answer5<<endl;
    	TrueFalse5 = (result5 == answer5);
    
    	total = TrueFalse1 + TrueFalse2 + TrueFalse3 + TrueFalse4 + TrueFalse5;
    	cout<< "You answered "<<total<<" correctly.\n";
    	return 0;
    
    }

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
    #include <time.h>
    #include <stdlib.h>
    
    srand(time(NULL));
    Call it once at the begine of your program.
    These basic questions are covered in the FAQ

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    Thanks for the advice, I look into the faq, but I could not solve the problem (I got the same values every time I ran the programm). I am a real Newbie to programming in C++. Could someone please help me a litter further.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Put your current program aside, and come up with the smallest and simplest program that has this problem. An example would be a program that prints just 5 pseudo-random numbers. Run it a few times, and see if you still get the same values every time the program is run.
    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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post your code showing your call to srand();
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    I don't know where to put the srand(), so it does not show the same numbers everytime. Here is the simple random number program, but I don't know how to make it so a user does not have to enter a seed number in order of it to generate random number and how can a generate a random number to a defined variable that equals a random number.

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
    	unsigned seed,y;
    	cout<< "Enter seed\n";
    	cin >> seed;
    	srand(seed);
    
    
    	y=1+rand()%50;
    	cout<<y<<endl;
    	return 0;
    
    }

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Try this:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    int main() {
        srand(time(0));
    
        cout << (rand() % 100) << endl;
    
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I don't know where to put the srand()
    The FAQ seemed pretty clear to me!

    "If you want to get different random numbers each time you run your program, you will also need to use the srand() function"

    "To get different random number set, pass a different seed. The most common way to achieve this is by calling the time() function and passing its result to srand()"

    And there's an example right there, showing
    srand(time(NULL));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    you also might try a different function set, rand isn't exactly the best function out there.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What's a better one?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    Thanks guys I got it to work!

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Be sure to cast time(0) to what ever type srand takes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Criteria based random numbers
    By alkisclio in forum C++ Programming
    Replies: 6
    Last Post: 09-14-2006, 12:29 PM
  4. Random Number Generating
    By K.n.i.g.h.t in forum C Programming
    Replies: 9
    Last Post: 01-30-2005, 02:16 PM
  5. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM