# Thread: Help with displaying random numbers

1. ## 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. Originally Posted by Bumps
[...]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. Code:
```cout << Random1 << " " << Random2 << " " << Random3 << "\n"
cout << __min(Random1 , __min(Random2 , Random3)) << "\n";```

4. Originally Posted by Bumps
So far I can generate the numbers but my only problem is I can't figure out how to display them in a row.
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.

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. 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).

7. Originally Posted by Bumps
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.

8. ## 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. 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.

10. 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. 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. 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.

13. 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.