Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find...

19
Functions (2) ISNE 261102 Faculty of Engineering Chiang Mai University

Transcript of Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find...

Page 1: Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result

Functions(2)

ISNE261102FacultyofEngineeringChiangMaiUniversity

Page 2: Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result

C++StandardLibraryHeaders

• TheC++StandardLibraryhasitsownheader.• Theheaderscontainthefunctionprototypesfortherelatedfunctionsthatformthelibrary.

• Theheadersalsocontaindefinitionsofvariousclasstypesandfunctions,aswellasconstantsneededbythosefunctions.

• Aheaderinstructs thecompileronhowtointerfacewithlibraryanduser-writtencomponents.

Page 3: Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result

C++StandardLibraryHeaders

• Example,<iomanip>

- setw()- setprecision()

Page 4: Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result

C++StandardLibraryHeaderssetw( n )

– Setnumberofcharacterstobeusedasfieldwidth(n).– n isaninteger

Page 5: Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result

C++StandardLibraryHeadersfixed << setprecision( m )

– Setdecimalprecision(m).– m isaninteger

Howabout fixed << setprecision(15) << f ?

Page 6: Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result

RandomNumberGeneration

• Theelementofchance:Acoreelementofcomputersimulationsandgames.

• Theelementofchancecanbeimplementedusingthestandardlibraryfunctionrand.

• Considerthefollowingstatement:i = rand();– Thefunctionrand() generatesanunsignedintegerbetween0 and32,767

Page 7: Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result

RandomNumberGeneration

• Whatifwewanttospecifytherangeofrandominteger?– Useofthemodulo(%)

Page 8: Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result

RandomNumberGeneration

• Todemonstraterand,let’sdevelopaprogramtosimulate20rollsofasix-sideddieandprintthevalueofeachroll.

rand() functionbelongsto<cstdlib>

Page 9: Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result

RandomNumberGeneration

Page 10: Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result

RandomNumberGeneration

• Let’ssimulate6,000rollsofadie!

Page 11: Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result

Thedefault caseisoptionalso,canbeignoredhere.

Page 12: Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result

RandomNumberGeneration• Functionrand actuallygeneratespseudorandomnumbers (deterministicrandomnumbers).Repeatedlycallingrand producesasequenceofnumbersthatrepeatsitselfeachtimetheprogramexecutes.

• Onceaprogramhasbeenthoroughlydebugged,itcanbeconditionedtoproduceadifferentsequenceofrandomnumbersforeachexecution.

• Thisiscalledrandomizing andisaccomplishedwiththeC++StandardLibraryfunctionsrand.

• Functionsrand takesanunsignedintegerargumentandseedstherand function toproduceadifferentsequenceofrandomnumbersforeachexecution.

Page 13: Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result

Usingsrand()

Page 14: Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result

Usingsrand()• Noticethattheprogramproducesadifferent sequenceofrandomnumberseachtimeitexecutes,providedthattheuserentersadifferentseed.

• Weusedthesameseed inthefirstandthirdsampleoutputs,sothesameseries of10numbersisdisplayedineachofthoseoutputs.

• Torandomizewithouthavingtoenteraseedeachtime,wemayuseastatementlike

Page 15: Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result

Usingsrand()• Thiscausesthecomputertoreaditsclocktoobtainthevaluefortheseed.

• Functiontime (withtheargument0aswrittenintheprecedingstatement)typicallyreturnsthecurrenttime asthenumberofsecondssinceJanuary1,1970,atmidnight GreenwichMeanTime(GMT).

• Thisvalueisconvertedtoanunsignedintegerandusedastheseed totherandomnumbergenerator.

• Thefunctionprototypefortimeisin<ctime>.

Page 16: Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result

Usingsrand()The6,000rollsofadieexamplewithsrand(time(0)).

Page 17: Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result

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.

Page 18: Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result

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.

Page 19: Lect7 HeaderFiles rand - Chiang Mai Universitysanti/cpe102_files/Lect7... · 2018-07-19 · 1. Find Mode, Median, Mean, and Standard Deviation (to 3 decimal points) of the result

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.