VB icon

Generate REAL Random Numbers

Email
Submitted on: 1/3/2015 6:38:00 AM
By: Mike Dirolf (from psc cd)  
Level: Beginner
User Rating: By 4 Users
Compatibility: C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 380
 
     //If you havent yet realized it the //function 'rand()' will not produce //an actual random number because it //uses the same seed every time, the //way to fix this is to use time as //a seed for the rand function. If //this helps, rate it please.
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :Generate REAL Random Numbers
//**************************************
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Generate REAL Random Numbers
// Description://If you havent yet realized it the 
//function 'rand()' will not produce 
//an actual random number because it 
//uses the same seed every time, the 
//way to fix this is to use time as
//a seed for the rand function. If 
//this helps, rate it please.
// By: Mike Dirolf (from psc cd)
//
// Returns:random number
//**************************************

//Real Random Numbers
#include <iostream.h> //for the output
#include <stdlib.h> //for 'rand', 'srand' 
#include <time.h> //for 'time'
int main()
 {
 //use time as seed for 'rand'
 srand(time(0)); 
 //create an actually random number
 int a=rand();
 cout<<a<<endl;
 system('pause');
 return 0;
 }
//if you compile and run this program 
//you will see that every time you 
//run it there will be a new, random 
//number. if you want a number in a 
//given range(lets say 0-8) replace 
//the call 'rand();' with 'rand()%9' 
//(notice we use 9, not 8.
//P.S. try running the same program
//I used without setting srand to time
// if you run it several times you will
//notice your output is the same every
//time!!!


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.