SlideShare a Scribd company logo
CBSE, Grade 12 
Computer Science 
Random Numbers - Notes 
Malathi Senthil, 
senthil.malathi@gmail.com 
9 5 56 
34 10 22 
7 
1 6 8 9
1. Why do we need to generate 
random numbers? 
 Have you ever played the game “TEMPLE RUN”? 
 Hope you noticed that the path run is not the same every 
time the game is played. 
 Yes, the path is randomly picked up. 
 What about the path and coins in the game “SUBWAY 
SURFERS”? 
 The path and coins are at random as well. 
 Other typical examples would be 
 Tossing coin in cricket 
 Rolling dice in board game
2. How to generate a random 
number? 
 Header file required – stdlib.h 
 Random(n) generates any random number from 0 to n-1 
 Example # 1 
int r = random(4); 
Possible values for r are: 
 3 
 2 
 1 
 0 
 Example # 2 
int r = random(0); 
The only possible value for r is zero (0) 
 The minimum number generated by random(n) is zero 
(0) 
 The maximum number generated by random(n) is (n-1)
3. Tossing a coin 
 Let us assume that 0 indicates head and 1 indicates tail. 
 To simulate the toss of a coin with this assumption, the 
following code is required: 
#include<iostream.h> 
#include<stdlib.h> 
#include<conio.h> 
#include<stdio.h> 
void main() 
{ 
int toss; 
toss = random(2); 
//Possible values for toss are 0 and 1 
if (toss==0) 
cout<<“Head”; 
else 
cout<<“Tail”; 
getchar(); 
}
4. Rolling a dice 
 When a dice is rolled, the possible outcomes are 
1,2,3,4,5 or 6 at random. Nobody can really predict the 
outcome. 
 Sample source for rolling a dice is given below (header 
files are excluded) 
void main() 
{ 
int dice; 
dice = random(6)+1; 
//Possible values for dice are 1,2,3,4,5 or 6 
cout<<“The number rolled is ”<<dice; 
getchar(); 
} 
 Random(6) generates number 0,1,2,3,4,5 at random 
 1 is added to random(6) such that 0 is not generated 
and 1 is the minimum number generated.
5. How to generate 3 consecutive 
random numbers? 
 Simply have for loop to do the job 
void main() 
{ 
int how_many=3, dice; 
for(int i=0;i<how_many;i++); 
{ 
dice = random(6)+1; 
//Possible values for dice are 1,2,3,4,5 or 6 
cout<<“The number rolled is ”<<dice; 
} 
getchar(); 
} 
 The problem with the above code is that we get the 
same sequence of random numbers every time we 
execute
6. How to get numbers at 
random for every execution? 
 Use randomize() function before calling random(6) 
 Randomize() function initializes random number generator 
with a random value 
void main() 
{ 
int how_many=3, dice; 
randomize(); 
for(int i=0;i<how_many;i++); 
{ 
dice = random(6)+1; 
//Possible values for dice are 1,2,3,4,5 or 6 
cout<<“The number rolled is ”<<dice; 
} 
getchar(); 
}
7. How to pick up one of your 
favourite geeks at random? 
 The example below is a simple one to pick up one of the 
3 geeks stored in an array. 
void main() 
{ 
char my_geeks[][20]={“Steve Jobs”,”Bill Gates”,”Larry Page”}; 
int rnd; 
randomize(); 
rnd = random(3); 
//Possible values for rnd are 0,1 or 2 
cout<<“My favourite geek is ”<<my_geeks[rnd]; 
//One of the geeks will be picked up at random 
getchar(); 
}
8. How to pick up 2 of my 
favourite cricketers at random? 
 For loop will rescue us now 
void main() 
{ 
char cricketers[][20]={“Dhoni”,”Kohli”}; 
int rnd_player; 
randomize(); 
for (int i=0;i<2;i++) 
{ 
rnd_player = random(2); 
//Possible values for rnd_friend are 0 or 1 
cout<<cricketers[rnd_player]<<“#”; 
//Same player may be picked up in 
//the 2nd iteration 
} 
getchar(); 
} 
 Possible outcomes are: 
 Dhoni#Dhoni# 
 Kohli#Kohli# 
 Dhoni#Kohli# 
 Kohli#Dhoni#
9. Rec@p... 
 stdlib.h is the header file required 
 random(n) generates a random number between 0 and 
n-1 
 randomize() function is used to initiate random number 
generator with a random value 
9 5 56 
34 10 22 
7 
1 6 8 9

More Related Content

What's hot

Down the rabbit hole, profiling in Django
Down the rabbit hole, profiling in DjangoDown the rabbit hole, profiling in Django
Down the rabbit hole, profiling in DjangoRemco Wendt
 
Bai Giang 11
Bai Giang 11Bai Giang 11
Bai Giang 11nbb3i
 
The Ring programming language version 1.5.2 book - Part 51 of 181
The Ring programming language version 1.5.2 book - Part 51 of 181The Ring programming language version 1.5.2 book - Part 51 of 181
The Ring programming language version 1.5.2 book - Part 51 of 181Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 54 of 189
The Ring programming language version 1.6 book - Part 54 of 189The Ring programming language version 1.6 book - Part 54 of 189
The Ring programming language version 1.6 book - Part 54 of 189Mahmoud Samir Fayed
 
Maze solving app listing
Maze solving app listingMaze solving app listing
Maze solving app listingChris Worledge
 
Java Unicode with Cool GUI Examples
Java Unicode with Cool GUI ExamplesJava Unicode with Cool GUI Examples
Java Unicode with Cool GUI ExamplesOXUS 20
 
Java Unicode with Live GUI Examples
Java Unicode with Live GUI ExamplesJava Unicode with Live GUI Examples
Java Unicode with Live GUI ExamplesAbdul Rahman Sherzad
 
The Ring programming language version 1.5.4 book - Part 52 of 185
The Ring programming language version 1.5.4 book - Part 52 of 185The Ring programming language version 1.5.4 book - Part 52 of 185
The Ring programming language version 1.5.4 book - Part 52 of 185Mahmoud Samir Fayed
 
Splinter: testando web apps
Splinter: testando web appsSplinter: testando web apps
Splinter: testando web appsdouglascamata
 
Dip Your Toes In The Sea Of Security (PHPNW16)
Dip Your Toes In The Sea Of Security (PHPNW16)Dip Your Toes In The Sea Of Security (PHPNW16)
Dip Your Toes In The Sea Of Security (PHPNW16)James Titcumb
 
Dip Your Toes in the Sea of Security
Dip Your Toes in the Sea of SecurityDip Your Toes in the Sea of Security
Dip Your Toes in the Sea of SecurityJames Titcumb
 
The Ring programming language version 1.3 book - Part 42 of 88
The Ring programming language version 1.3 book - Part 42 of 88The Ring programming language version 1.3 book - Part 42 of 88
The Ring programming language version 1.3 book - Part 42 of 88Mahmoud Samir Fayed
 
Dip Your Toes in the Sea of Security (IPC Fall 2017)
Dip Your Toes in the Sea of Security (IPC Fall 2017)Dip Your Toes in the Sea of Security (IPC Fall 2017)
Dip Your Toes in the Sea of Security (IPC Fall 2017)James Titcumb
 

What's hot (20)

Down the rabbit hole, profiling in Django
Down the rabbit hole, profiling in DjangoDown the rabbit hole, profiling in Django
Down the rabbit hole, profiling in Django
 
3 1-1
3 1-13 1-1
3 1-1
 
Bai Giang 11
Bai Giang 11Bai Giang 11
Bai Giang 11
 
The Ring programming language version 1.5.2 book - Part 51 of 181
The Ring programming language version 1.5.2 book - Part 51 of 181The Ring programming language version 1.5.2 book - Part 51 of 181
The Ring programming language version 1.5.2 book - Part 51 of 181
 
New
NewNew
New
 
The Ring programming language version 1.6 book - Part 54 of 189
The Ring programming language version 1.6 book - Part 54 of 189The Ring programming language version 1.6 book - Part 54 of 189
The Ring programming language version 1.6 book - Part 54 of 189
 
project3
project3project3
project3
 
Maze solving app listing
Maze solving app listingMaze solving app listing
Maze solving app listing
 
Java Unicode with Cool GUI Examples
Java Unicode with Cool GUI ExamplesJava Unicode with Cool GUI Examples
Java Unicode with Cool GUI Examples
 
Java Unicode with Live GUI Examples
Java Unicode with Live GUI ExamplesJava Unicode with Live GUI Examples
Java Unicode with Live GUI Examples
 
Transmogrify
TransmogrifyTransmogrify
Transmogrify
 
The Ring programming language version 1.5.4 book - Part 52 of 185
The Ring programming language version 1.5.4 book - Part 52 of 185The Ring programming language version 1.5.4 book - Part 52 of 185
The Ring programming language version 1.5.4 book - Part 52 of 185
 
Splinter: testando web apps
Splinter: testando web appsSplinter: testando web apps
Splinter: testando web apps
 
PHP 5.4
PHP 5.4PHP 5.4
PHP 5.4
 
Game z
Game zGame z
Game z
 
Dip Your Toes In The Sea Of Security (PHPNW16)
Dip Your Toes In The Sea Of Security (PHPNW16)Dip Your Toes In The Sea Of Security (PHPNW16)
Dip Your Toes In The Sea Of Security (PHPNW16)
 
Dip Your Toes in the Sea of Security
Dip Your Toes in the Sea of SecurityDip Your Toes in the Sea of Security
Dip Your Toes in the Sea of Security
 
The Ring programming language version 1.3 book - Part 42 of 88
The Ring programming language version 1.3 book - Part 42 of 88The Ring programming language version 1.3 book - Part 42 of 88
The Ring programming language version 1.3 book - Part 42 of 88
 
Dip Your Toes in the Sea of Security (IPC Fall 2017)
Dip Your Toes in the Sea of Security (IPC Fall 2017)Dip Your Toes in the Sea of Security (IPC Fall 2017)
Dip Your Toes in the Sea of Security (IPC Fall 2017)
 
CGI.pm - 3ло?!
CGI.pm - 3ло?!CGI.pm - 3ло?!
CGI.pm - 3ло?!
 

Viewers also liked

C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board ExamsC++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Examshishamrizvi
 
CBSE XII Communication And Network Concepts
CBSE XII Communication And Network ConceptsCBSE XII Communication And Network Concepts
CBSE XII Communication And Network ConceptsGuru Ji
 
CBSE XII Boolean Algebra
CBSE XII Boolean AlgebraCBSE XII Boolean Algebra
CBSE XII Boolean AlgebraGuru Ji
 
+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II NotesAndrew Raj
 
12th CBSE Practical File
12th CBSE Practical File12th CBSE Practical File
12th CBSE Practical FileAshwin Francis
 
Boolean algebra
Boolean algebraBoolean algebra
Boolean algebraGagan Deep
 
CBSE XII Database Concepts And MySQL Presentation
CBSE XII Database Concepts And MySQL PresentationCBSE XII Database Concepts And MySQL Presentation
CBSE XII Database Concepts And MySQL PresentationGuru Ji
 
Computer Science Investigatory Project Class 12
Computer Science Investigatory Project Class 12Computer Science Investigatory Project Class 12
Computer Science Investigatory Project Class 12Self-employed
 
Pe 4030 digital logic chapter 7 (weeks 11 12)
Pe 4030 digital logic chapter 7 (weeks 11 12)Pe 4030 digital logic chapter 7 (weeks 11 12)
Pe 4030 digital logic chapter 7 (weeks 11 12)Charlton Inao
 
Computer Practical
Computer PracticalComputer Practical
Computer PracticalPLKFM
 
CBSE Grade12, Computer Science, Sample Question Paper
CBSE Grade12, Computer Science, Sample Question PaperCBSE Grade12, Computer Science, Sample Question Paper
CBSE Grade12, Computer Science, Sample Question PaperMalathi Senthil
 
Number system
Number systemNumber system
Number systemaviban
 
Python for class 11 (CBSE Computer science sub code 083)
Python for class 11 (CBSE Computer science sub code 083)Python for class 11 (CBSE Computer science sub code 083)
Python for class 11 (CBSE Computer science sub code 083)Nitin Kumar
 
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVSCBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVSGautham Rajesh
 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Deepak Singh
 
PROPRIETARY AND OPEN SOURCE SOFTWARE
PROPRIETARY AND OPEN SOURCE SOFTWARE PROPRIETARY AND OPEN SOURCE SOFTWARE
PROPRIETARY AND OPEN SOURCE SOFTWARE Kak Yong
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arorakulachihansraj
 

Viewers also liked (20)

Computer practicals(part) Class 12
Computer practicals(part) Class 12Computer practicals(part) Class 12
Computer practicals(part) Class 12
 
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board ExamsC++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
 
CBSE XII Communication And Network Concepts
CBSE XII Communication And Network ConceptsCBSE XII Communication And Network Concepts
CBSE XII Communication And Network Concepts
 
CBSE XII Boolean Algebra
CBSE XII Boolean AlgebraCBSE XII Boolean Algebra
CBSE XII Boolean Algebra
 
+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes
 
12th CBSE Practical File
12th CBSE Practical File12th CBSE Practical File
12th CBSE Practical File
 
Boolean algebra
Boolean algebraBoolean algebra
Boolean algebra
 
CBSE XII Database Concepts And MySQL Presentation
CBSE XII Database Concepts And MySQL PresentationCBSE XII Database Concepts And MySQL Presentation
CBSE XII Database Concepts And MySQL Presentation
 
Computer Science Investigatory Project Class 12
Computer Science Investigatory Project Class 12Computer Science Investigatory Project Class 12
Computer Science Investigatory Project Class 12
 
Pe 4030 digital logic chapter 7 (weeks 11 12)
Pe 4030 digital logic chapter 7 (weeks 11 12)Pe 4030 digital logic chapter 7 (weeks 11 12)
Pe 4030 digital logic chapter 7 (weeks 11 12)
 
Computer Practical
Computer PracticalComputer Practical
Computer Practical
 
CBSE Grade12, Computer Science, Sample Question Paper
CBSE Grade12, Computer Science, Sample Question PaperCBSE Grade12, Computer Science, Sample Question Paper
CBSE Grade12, Computer Science, Sample Question Paper
 
Number system
Number systemNumber system
Number system
 
Boolean expressions
Boolean expressionsBoolean expressions
Boolean expressions
 
Python for class 11 (CBSE Computer science sub code 083)
Python for class 11 (CBSE Computer science sub code 083)Python for class 11 (CBSE Computer science sub code 083)
Python for class 11 (CBSE Computer science sub code 083)
 
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVSCBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++
 
PROPRIETARY AND OPEN SOURCE SOFTWARE
PROPRIETARY AND OPEN SOURCE SOFTWARE PROPRIETARY AND OPEN SOURCE SOFTWARE
PROPRIETARY AND OPEN SOURCE SOFTWARE
 
Capital Rationing
Capital RationingCapital Rationing
Capital Rationing
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 

Similar to CBSE, Grade12, Computer Science, Random Numbers - Notes

Encryption and Decryption using Tag Design
Encryption and Decryption using Tag Design Encryption and Decryption using Tag Design
Encryption and Decryption using Tag Design Joe Jiang
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語ikdysfm
 
Explorando el Diseño de la Memoria en Rust
Explorando el Diseño de la Memoria en RustExplorando el Diseño de la Memoria en Rust
Explorando el Diseño de la Memoria en RustGermán Küber
 
Rootkit on Linux X86 v2.6
Rootkit on Linux X86 v2.6Rootkit on Linux X86 v2.6
Rootkit on Linux X86 v2.6fisher.w.y
 
Rootkit on linux_x86_v2.6
Rootkit on linux_x86_v2.6Rootkit on linux_x86_v2.6
Rootkit on linux_x86_v2.6scuhurricane
 
Самые вкусные баги из игрового кода: как ошибаются наши коллеги-программисты ...
Самые вкусные баги из игрового кода: как ошибаются наши коллеги-программисты ...Самые вкусные баги из игрового кода: как ошибаются наши коллеги-программисты ...
Самые вкусные баги из игрового кода: как ошибаются наши коллеги-программисты ...DevGAMM Conference
 
Python Peculiarities
Python PeculiaritiesPython Peculiarities
Python Peculiaritiesnoamt
 
functions2-200924082810.pdf
functions2-200924082810.pdffunctions2-200924082810.pdf
functions2-200924082810.pdfpaijitk
 
Reiterating JavaScript & Node.js iterators
Reiterating JavaScript & Node.js iteratorsReiterating JavaScript & Node.js iterators
Reiterating JavaScript & Node.js iteratorsLuciano Mammino
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]vikram mahendra
 
Beautiful python - PyLadies
Beautiful python - PyLadiesBeautiful python - PyLadies
Beautiful python - PyLadiesAlicia Pérez
 
AIOU Code 1429 Solved Assignment 1 Autumn 2022.pptx
AIOU Code 1429 Solved Assignment 1 Autumn 2022.pptxAIOU Code 1429 Solved Assignment 1 Autumn 2022.pptx
AIOU Code 1429 Solved Assignment 1 Autumn 2022.pptxZawarali786
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School ProgrammersSiva Arunachalam
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory archana singh
 
Keygenning using the Z3 SMT Solver
Keygenning using the Z3 SMT SolverKeygenning using the Z3 SMT Solver
Keygenning using the Z3 SMT Solverextremecoders
 
Arduino creative coding class part iii
Arduino creative coding class part iiiArduino creative coding class part iii
Arduino creative coding class part iiiJonah Marrs
 

Similar to CBSE, Grade12, Computer Science, Random Numbers - Notes (20)

Encryption and Decryption using Tag Design
Encryption and Decryption using Tag Design Encryption and Decryption using Tag Design
Encryption and Decryption using Tag Design
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
Explorando el Diseño de la Memoria en Rust
Explorando el Diseño de la Memoria en RustExplorando el Diseño de la Memoria en Rust
Explorando el Diseño de la Memoria en Rust
 
Rootkit on Linux X86 v2.6
Rootkit on Linux X86 v2.6Rootkit on Linux X86 v2.6
Rootkit on Linux X86 v2.6
 
Rootkit on linux_x86_v2.6
Rootkit on linux_x86_v2.6Rootkit on linux_x86_v2.6
Rootkit on linux_x86_v2.6
 
Самые вкусные баги из игрового кода: как ошибаются наши коллеги-программисты ...
Самые вкусные баги из игрового кода: как ошибаются наши коллеги-программисты ...Самые вкусные баги из игрового кода: как ошибаются наши коллеги-программисты ...
Самые вкусные баги из игрового кода: как ошибаются наши коллеги-программисты ...
 
Dalembert
DalembertDalembert
Dalembert
 
Python Peculiarities
Python PeculiaritiesPython Peculiarities
Python Peculiarities
 
functions2-200924082810.pdf
functions2-200924082810.pdffunctions2-200924082810.pdf
functions2-200924082810.pdf
 
Reiterating JavaScript & Node.js iterators
Reiterating JavaScript & Node.js iteratorsReiterating JavaScript & Node.js iterators
Reiterating JavaScript & Node.js iterators
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
 
Dev C++ Code Basic Loop
Dev C++ Code Basic LoopDev C++ Code Basic Loop
Dev C++ Code Basic Loop
 
DEV C++
DEV C++DEV C++
DEV C++
 
Beautiful python - PyLadies
Beautiful python - PyLadiesBeautiful python - PyLadies
Beautiful python - PyLadies
 
AIOU Code 1429 Solved Assignment 1 Autumn 2022.pptx
AIOU Code 1429 Solved Assignment 1 Autumn 2022.pptxAIOU Code 1429 Solved Assignment 1 Autumn 2022.pptx
AIOU Code 1429 Solved Assignment 1 Autumn 2022.pptx
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School Programmers
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
 
Keygenning using the Z3 SMT Solver
Keygenning using the Z3 SMT SolverKeygenning using the Z3 SMT Solver
Keygenning using the Z3 SMT Solver
 
cosc 281 hw2
cosc 281 hw2cosc 281 hw2
cosc 281 hw2
 
Arduino creative coding class part iii
Arduino creative coding class part iiiArduino creative coding class part iii
Arduino creative coding class part iii
 

Recently uploaded

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfSpecial education needs
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chipsGeoBlogs
 
Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...
Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...
Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...SachinKumar945617
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfThiyagu K
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfTamralipta Mahavidyalaya
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345beazzy04
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonSteve Thomason
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxDenish Jangid
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationDelapenabediema
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...Sayali Powar
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)rosedainty
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...Jisc
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfkaushalkr1407
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxRaedMohamed3
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...Nguyen Thanh Tu Collection
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersPedroFerreira53928
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePedroFerreira53928
 

Recently uploaded (20)

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...
Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...
Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 

CBSE, Grade12, Computer Science, Random Numbers - Notes

  • 1. CBSE, Grade 12 Computer Science Random Numbers - Notes Malathi Senthil, senthil.malathi@gmail.com 9 5 56 34 10 22 7 1 6 8 9
  • 2. 1. Why do we need to generate random numbers?  Have you ever played the game “TEMPLE RUN”?  Hope you noticed that the path run is not the same every time the game is played.  Yes, the path is randomly picked up.  What about the path and coins in the game “SUBWAY SURFERS”?  The path and coins are at random as well.  Other typical examples would be  Tossing coin in cricket  Rolling dice in board game
  • 3. 2. How to generate a random number?  Header file required – stdlib.h  Random(n) generates any random number from 0 to n-1  Example # 1 int r = random(4); Possible values for r are:  3  2  1  0  Example # 2 int r = random(0); The only possible value for r is zero (0)  The minimum number generated by random(n) is zero (0)  The maximum number generated by random(n) is (n-1)
  • 4. 3. Tossing a coin  Let us assume that 0 indicates head and 1 indicates tail.  To simulate the toss of a coin with this assumption, the following code is required: #include<iostream.h> #include<stdlib.h> #include<conio.h> #include<stdio.h> void main() { int toss; toss = random(2); //Possible values for toss are 0 and 1 if (toss==0) cout<<“Head”; else cout<<“Tail”; getchar(); }
  • 5. 4. Rolling a dice  When a dice is rolled, the possible outcomes are 1,2,3,4,5 or 6 at random. Nobody can really predict the outcome.  Sample source for rolling a dice is given below (header files are excluded) void main() { int dice; dice = random(6)+1; //Possible values for dice are 1,2,3,4,5 or 6 cout<<“The number rolled is ”<<dice; getchar(); }  Random(6) generates number 0,1,2,3,4,5 at random  1 is added to random(6) such that 0 is not generated and 1 is the minimum number generated.
  • 6. 5. How to generate 3 consecutive random numbers?  Simply have for loop to do the job void main() { int how_many=3, dice; for(int i=0;i<how_many;i++); { dice = random(6)+1; //Possible values for dice are 1,2,3,4,5 or 6 cout<<“The number rolled is ”<<dice; } getchar(); }  The problem with the above code is that we get the same sequence of random numbers every time we execute
  • 7. 6. How to get numbers at random for every execution?  Use randomize() function before calling random(6)  Randomize() function initializes random number generator with a random value void main() { int how_many=3, dice; randomize(); for(int i=0;i<how_many;i++); { dice = random(6)+1; //Possible values for dice are 1,2,3,4,5 or 6 cout<<“The number rolled is ”<<dice; } getchar(); }
  • 8. 7. How to pick up one of your favourite geeks at random?  The example below is a simple one to pick up one of the 3 geeks stored in an array. void main() { char my_geeks[][20]={“Steve Jobs”,”Bill Gates”,”Larry Page”}; int rnd; randomize(); rnd = random(3); //Possible values for rnd are 0,1 or 2 cout<<“My favourite geek is ”<<my_geeks[rnd]; //One of the geeks will be picked up at random getchar(); }
  • 9. 8. How to pick up 2 of my favourite cricketers at random?  For loop will rescue us now void main() { char cricketers[][20]={“Dhoni”,”Kohli”}; int rnd_player; randomize(); for (int i=0;i<2;i++) { rnd_player = random(2); //Possible values for rnd_friend are 0 or 1 cout<<cricketers[rnd_player]<<“#”; //Same player may be picked up in //the 2nd iteration } getchar(); }  Possible outcomes are:  Dhoni#Dhoni#  Kohli#Kohli#  Dhoni#Kohli#  Kohli#Dhoni#
  • 10. 9. Rec@p...  stdlib.h is the header file required  random(n) generates a random number between 0 and n-1  randomize() function is used to initiate random number generator with a random value 9 5 56 34 10 22 7 1 6 8 9