Help with displaying random numbers

This is a discussion on Help with displaying random numbers within the C++ Programming forums, part of the General Programming Boards category; Hello all new to programming in general and I'm currently taking my first C++ class. Everything has been going great ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    5

    Help with displaying random numbers

    Hello all
    new to programming in general and I'm currently taking my first C++ class. Everything has been going great until I began this assigment. I bascally have to create a program that genreates 3 random numbers (easy) all numbers need to be different (done), print the 3 number as well as the minimum of the three. It has to output like this:

    Enter an integer between 10 and 20:
    The random numbres are:
    The minimum number is:

    So far I can generate the numbers but my only problem is I can't figure out how to display them in a row. I also have partially figured out how to display the minimum of the three. Could any one give me some hints or tips?

    Code:
    #include <iostream>					
    #include <iomanip>					
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    int minimum(int, int , int);// function prototype
    int num1, num2, num3;
    
    int main()
    {
    	const int Numbers  = 3;    
    	float randvalue;
    	int NUM; 
    	
    
    	cout <<"Enter an integer between 10 and 20: ";
    	cin >> NUM; 
    	
    	srand(time(NULL));
    	for ( int counter = 1; counter <= Numbers; counter++ ) //will loop the number of the times the user entered
    	{
    		randvalue = rand ();
    		cout <<"The random numbers are: "<< ( 1 + rand() % NUM )<<endl;  //generates numbers from 1 to the number user entered
    	} 						
    
    	cout << "The minimum number is " << minimum(num1, num2, num3)<<endl;
    	   
      system ("pause");
      return 0;  
    }
    // Function minumum definition
    int minimum( int num1, int num2, int num3)
    {
    	int min = num1;
    	if ( num2 < min )
    	min = num2;
    	if ( num3 < min )
    	min = num3;
        return min;
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by Bumps View Post
    [...]I can't figure out how to display them in a row.
    do you mean you want to print them all on one line? right now you generate a number and print that whole string ("The random numbers...") as well as the number you just generated. if you want them on one line, then just use one print statement to show all three at once, rather than one print and line per number, i.e. "cout << "numbers are:" << num1 << num2 << num3 <<endl;" (cleaning up the formatting of course).

    your minimum function looks fine for your requirements (3 numbers). im assuming you havent talked about arrays yet, otherwise the program would be different. the problem that youre having is that you never set "num1", "num2", "num3" to anything. you declare them, but you dont assign them to the random number you generate. also, you make two "rand" calls. the first one ("randvalue = rand ();") doesnt really do anything, because you dont do anything with the value it returns (i.e. you never use "randvalue"). you should get rid of the "for" loop, and just have 3 lines, ie:
    Code:
    num1 = // assign it random number
    num2 = // assign it random number
    num3 = // assign it random number
    // print ("cout") all 3 of this on one line
    again, this is assuming you dont know arrays yet (otherwise it makes sense to use a different method).

    also, its best to minimize "global" variables. a "global" variable is a variable thats declared outside of any function (which is what you have), and im sure youll learn about what they are eventually. basically, move the "int num1, num2, num3;" declaration into the beginning of the "main" function.

    another thing, it looks like the first number you ask for is the "max", if you want it to behave correctly you should validate the input. for example if i enter "-5" as my max, it will generate positive numbers up to 5 (not <= -5). maybe force them to enter a positive maximum, or maybe this is isnt a concern in your assignment (though be aware of the problem and possible solutions!).

    hope it helps

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Code:
    cout << Random1 << " " << Random2 << " " << Random3 << "\n"
    cout << __min(Random1 , __min(Random2 , Random3)) << "\n";
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  4. #4
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Bumps View Post
    So far I can generate the numbers but my only problem is I can't figure out how to display them in a row.
    Quote Originally Posted by nadroj View Post
    do you mean you want to print them all on one line?
    I think he means sort them before printing?

    sort - C++ Reference

    Generate the random number, append it to the list, sort the list, iterate the list and print them out. Then again maybe he's not allowed to use the STL? If so, he could simply use std::min instead of creating his own minimum function (min(num1, min(num2, num3))). But that's apart of the assignment I guess. In that case it's just a Google search away.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    Thank you nadroj and everyone else who helped me out.

    I don't know why I was using the "for" loop statement. I guess I was making the assignment harder than it was. The funny thing is we learned about arrays today but the professor said not to use them for this assignment. Thanks for the tip on moving my global variable. I know its bad practice but it just slipped my mind.

    Just out of curiosity the three random numbers are different but on occasion two of the three end up being the same. Would an if statement fix this. Ill go try this out when I get home.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Just out of curiosity the three random numbers are different but on occasion two of the three end up being the same. Would an if statement fix this. Ill go try this out when I get home.
    You'll need a loop (simplest to loop while there's a pair of equal values).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by Bumps View Post
    Just out of curiosity the three random numbers are different but on occasion two of the three end up being the same. Would an if statement fix this. Ill go try this out when I get home.
    Yes, a 6 is just as likely as any other number to come up after you roll a 6 (or any other number). A truly random generator does not remember the previous rolls. This is why so many people lose money at roulette, they think if a number hasn't come up in awhile that it is more likely to come up in the future, it is not. What they don't realize is that the croupier can effect the numbers that come up through a technique called 'sectioning'. Whether they are doing this intentionally or not depends on the croupier. If they do it intentionally, it makes them a better croupier, if they are doing it unconsciously it makes them worse.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    Smile Final program!!!

    Hey bumps how does the final program look like??? Hav something similar to that and need help. If u could post it, it would be most helpful.

    Thanks

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you really can't figure it out from his original code, and everything described here, then you need far more than "help". You need well... exactly what you're asking for: someone to do it for you. Nice try, but no. Make a new thread, put YOUR code in it, and describe your problem.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    2
    I am a newbie to C++ programing and I think, I am working on same assignment and stuck on same problem i.e sometimes I do get 2 same random numbers out of three, when I enter same number again and again but sometimes I don't . IF i do use a loop, do I use rand() inside the loop or do I use "IF" statement.


    Thank you.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    you use "rand()" whenever you want a new random number. if you want a random number each iteration of some loop then put it inside the loop, otherwise dont. it doesnt really have anything to do with an "if" block.

    ps: how many people are in your class?!

  12. #12
    Registered User
    Join Date
    Oct 2009
    Posts
    2
    I have other question, If I use sleep() with windows.h library, will it help to change the seeding of the random number? I mean srand( time(Null)) changes with time so If i use sleep(1000) will it help me to avoid the likelihood of getting 2 same numbers?
    I added it and see the lag between getting those 3 numbers but it does not solve my problem. Any ideas?




    It is an online class with close to 50 students I guess.
    Last edited by milkyandromeda; 10-03-2009 at 01:01 PM.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    if you sleep for one second then do "srand(time(null))" your numbers wont be any more "random", the seed will simply be generated from the time one second later than not doing the sleep. if you make sure you generate the seed once from the current (or whatever) time then your numbers will be random, i.e. any 2 numbers are equally likely to appear.

    if you could control what "random" numbers appear or their frequency (probability), you would be a millionaire gambler.

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, 09:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 10-24-2008, 02:22 PM
  4. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  5. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-12-2003, 12:03 AM

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