View Single Post
Join Date: Feb 2005
Posts: 84
Reputation: evilsilver is an unknown quantity at this point 
Solved Threads: 1
evilsilver's Avatar
evilsilver evilsilver is offline Offline
Junior Poster in Training

Re: C++ Random Numbers

 
0
  #9
Feb 7th, 2005
Originally Posted by aylina
my question is, for the functions on choosing random numbers in a specific range, what should i do to make the cout one by one?
As in, like the function given above, it gives the whole 20 outputs at once, but what i would like to do is to get the output one by one, how could i do that?
thank you
one way to do this is to use the getch(); you can put it in the for loop right after the cout command. this will have the computer pause after each display of the number, pressing any key will make the computer continue. (you will also have to include #include <conio> to run the getch(); function.)
for example:

  1. #include <iostream>
  2. #include <conio>
  3. #include <ctime>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. srand((unsigned)time(0));
  11. int random_integer;
  12. int lowest=1, highest=10;
  13. int range=(highest-lowest)+1;
  14. for(int index=0; index<20; index++){
  15. random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
  16. cout << random_integer << endl;
  17. getch();
  18. }
  19. }