Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find...
Transcript of Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find...
Functions(2)
ISNE261102FacultyofEngineeringChiangMaiUniversity
C++StandardLibraryHeaders
• TheC++StandardLibraryhasitsownheader.• Theheaderscontainthefunctionprototypesfortherelatedfunctionsthatformthelibrary.
• Theheadersalsocontaindefinitionsofvariousclasstypesandfunctions,aswellasconstantsneededbythosefunctions.
• Aheaderinstructs thecompileronhowtointerfacewithlibraryanduser-writtencomponents.
C++StandardLibraryHeaders
• Example,<iomanip>
- setw()- setprecision()
C++StandardLibraryHeaderssetw( n )
– Setnumberofcharacterstobeusedasfieldwidth(n).– n isaninteger
C++StandardLibraryHeadersfixed << setprecision( m )
– Setdecimalprecision(m).– m isaninteger
Howabout fixed << setprecision(15) << f ?
RandomNumberGeneration
• Theelementofchance:Acoreelementofcomputersimulationsandgames.
• Theelementofchancecanbeimplementedusingthestandardlibraryfunctionrand.
• Considerthefollowingstatement:i = rand();– Thefunctionrand() generatesanunsignedintegerbetween0 and32,767
RandomNumberGeneration
• Whatifwewanttospecifytherangeofrandominteger?– Useofthemodulo(%)
RandomNumberGeneration
• Todemonstraterand,let’sdevelopaprogramtosimulate20rollsofasix-sideddieandprintthevalueofeachroll.
rand() functionbelongsto<cstdlib>
RandomNumberGeneration
RandomNumberGeneration
• Let’ssimulate6,000rollsofadie!
Thedefault caseisoptionalso,canbeignoredhere.
RandomNumberGeneration• Functionrand actuallygeneratespseudorandomnumbers (deterministicrandomnumbers).Repeatedlycallingrand producesasequenceofnumbersthatrepeatsitselfeachtimetheprogramexecutes.
• Onceaprogramhasbeenthoroughlydebugged,itcanbeconditionedtoproduceadifferentsequenceofrandomnumbersforeachexecution.
• Thisiscalledrandomizing andisaccomplishedwiththeC++StandardLibraryfunctionsrand.
• Functionsrand takesanunsignedintegerargumentandseedstherand function toproduceadifferentsequenceofrandomnumbersforeachexecution.
Usingsrand()
Usingsrand()• Noticethattheprogramproducesadifferent sequenceofrandomnumberseachtimeitexecutes,providedthattheuserentersadifferentseed.
• Weusedthesameseed inthefirstandthirdsampleoutputs,sothesameseries of10numbersisdisplayedineachofthoseoutputs.
• Torandomizewithouthavingtoenteraseedeachtime,wemayuseastatementlike
Usingsrand()• Thiscausesthecomputertoreaditsclocktoobtainthevaluefortheseed.
• Functiontime (withtheargument0aswrittenintheprecedingstatement)typicallyreturnsthecurrenttime asthenumberofsecondssinceJanuary1,1970,atmidnight GreenwichMeanTime(GMT).
• Thisvalueisconvertedtoanunsignedintegerandusedastheseed totherandomnumbergenerator.
• Thefunctionprototypefortimeisin<ctime>.
Usingsrand()The6,000rollsofadieexamplewithsrand(time(0)).
LabAssignment#6
1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result obtained in 6,000-rolled dice example. Print the results on the screen.
2. Generate 10 random numbers between 1 and 2 (to 4 decimal points), for example, 1.1234, 1.8888, and find the sum, average, and standard deviation of these numbers. Print the random numbers and the results on the screen.
LabAssignment#6
1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result obtained in 6,000-rolled dice example. Print the results on the screen.
LabAssignment#6
2. Generate 10 random numbers between 1 and 2 (to 4 decimal points), for example, 1.1234, 1.8888, and find the sum, average, and standard deviation of these numbers. Print the random numbers and the results on the screen.