Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 99,781 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,545 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




Random Numbers

Shows you how to use random numbers.

Submitted By: BlackJesus
Actions:
Rating:
Views: 18,913

Language: C++

Last Modified: June 14, 2006
Instructions: Input a number between 1 and 10. It will output that many random numbers from 1 - 50.

Snippet


  1. /*This code is used to get a random number
  2.   between 1 and 50.  User inputs how many
  3.   numbers they want, between 1 and 10.    */
  4.  
  5.  
  6. #include <iostream.h>                    //Needed for input/output
  7. #include <time.h>                         //Need this so random will work
  8. #include <stdlib.h>                         //Need this for random functions
  9.  
  10. void main()
  11. {
  12.      srand(time(0));                         //Gets internal clock time for random
  13.  
  14.      int Rand_Num;                         //Variable for random number
  15.      int Input;                              //Variable for Input
  16.      
  17. Rechoose:                                   //Rechoose subroutine
  18.      cout << "Enter a number 1 - 10: ";     //Prompt user
  19.      cin >> Input;                              //Get input from user
  20.      if(Input >= 1 && Input <= 10)          //Checks if greater than 1 and less than 10
  21.      {
  22.           for(int i = 0; i < Input; i++)     //Outputs a random number up until number of user input
  23.           {
  24.                Rand_Num = (rand() % 50) + 1;     //Makes Rand_Num a random number, the + 1 makes it so it is 1+ and not 0+
  25.                cout << Rand_Num << '\n';       //Outputs random number
  26.           }
  27.      }
  28.      else
  29.      {
  30.           goto Rechoose;                              //If input isnt between 1 and 10 then makes user rechoose
  31.      }
  32. }
  33.  

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->