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

PDF
PyCon2009_AI_Alt
TXT
Bubble archery game(c program)
PDF
穏やかにファイルを削除する
PDF
Climbing the Abstract Syntax Tree (PHP South Africa 2017)
PDF
That Goes Without Alpha-Num (or Does It ?) all your base10 are belong to us
PDF
Dip Your Toes in the Sea of Security (PHP South Africa 2017)
PDF
MoSQL: More than SQL, but Less than ORM @ PyCon APAC 2013
PDF
League of Graphs
PyCon2009_AI_Alt
Bubble archery game(c program)
穏やかにファイルを削除する
Climbing the Abstract Syntax Tree (PHP South Africa 2017)
That Goes Without Alpha-Num (or Does It ?) all your base10 are belong to us
Dip Your Toes in the Sea of Security (PHP South Africa 2017)
MoSQL: More than SQL, but Less than ORM @ PyCon APAC 2013
League of Graphs

What's hot (20)

PPTX
Down the rabbit hole, profiling in Django
DOCX
3 1-1
PDF
Bai Giang 11
PDF
The Ring programming language version 1.5.2 book - Part 51 of 181
PDF
The Ring programming language version 1.6 book - Part 54 of 189
RTF
project3
PDF
Maze solving app listing
PDF
Java Unicode with Cool GUI Examples
PDF
Java Unicode with Live GUI Examples
KEY
Transmogrify
PDF
The Ring programming language version 1.5.4 book - Part 52 of 185
KEY
Splinter: testando web apps
PPTX
Game z
PDF
Dip Your Toes In The Sea Of Security (PHPNW16)
PDF
Dip Your Toes in the Sea of Security
PDF
The Ring programming language version 1.3 book - Part 42 of 88
PDF
Dip Your Toes in the Sea of Security (IPC Fall 2017)
PDF
CGI.pm - 3ло?!
Down the rabbit hole, profiling in Django
3 1-1
Bai Giang 11
The Ring programming language version 1.5.2 book - Part 51 of 181
The Ring programming language version 1.6 book - Part 54 of 189
project3
Maze solving app listing
Java Unicode with Cool GUI Examples
Java Unicode with Live GUI Examples
Transmogrify
The Ring programming language version 1.5.4 book - Part 52 of 185
Splinter: testando web apps
Game z
Dip Your Toes In The Sea Of Security (PHPNW16)
Dip Your Toes in the Sea of Security
The Ring programming language version 1.3 book - Part 42 of 88
Dip Your Toes in the Sea of Security (IPC Fall 2017)
CGI.pm - 3ло?!
Ad

Viewers also liked (20)

PDF
Computer practicals(part) Class 12
PDF
CBSE XII Communication And Network Concepts
PDF
CBSE XII Boolean Algebra
PDF
+2 Computer Science - Volume II Notes
DOCX
12th CBSE Practical File
PPTX
Boolean algebra
PDF
CBSE XII Database Concepts And MySQL Presentation
PDF
Computer Science Investigatory Project Class 12
PPT
Pe 4030 digital logic chapter 7 (weeks 11 12)
PDF
Computer Practical
RTF
CBSE Grade12, Computer Science, Sample Question Paper
PPTX
Number system
PPTX
Boolean expressions
PDF
Python for class 11 (CBSE Computer science sub code 083)
PDF
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
PDF
Revision notes for exam 2011 computer science with C++
PPT
PROPRIETARY AND OPEN SOURCE SOFTWARE
DOC
Capital Rationing
PPT
Stacks & Queues By Ms. Niti Arora
PDF
School Management (c++)
Computer practicals(part) Class 12
CBSE XII Communication And Network Concepts
CBSE XII Boolean Algebra
+2 Computer Science - Volume II Notes
12th CBSE Practical File
Boolean algebra
CBSE XII Database Concepts And MySQL Presentation
Computer Science Investigatory Project Class 12
Pe 4030 digital logic chapter 7 (weeks 11 12)
Computer Practical
CBSE Grade12, Computer Science, Sample Question Paper
Number system
Boolean expressions
Python for class 11 (CBSE Computer science sub code 083)
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
Revision notes for exam 2011 computer science with C++
PROPRIETARY AND OPEN SOURCE SOFTWARE
Capital Rationing
Stacks & Queues By Ms. Niti Arora
School Management (c++)
Ad

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

PPT
lecture56.ppt
PPT
lecture56functionsggggggggggggggggggg.ppt
PPTX
Random numbers c++ class 11 and 12
PPTX
C++ Homework Help
PPT
Function in cpu 1
PPTX
Chp7_C++_Functions_Part1_Built-in functions.pptx
PPTX
First game using c language
PPT
FP 201 - Unit 3 Part 2
PDF
54602399 c-examples-51-to-108-programe-ee01083101
PPT
C chap05
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
PPTX
CMSC 201 - Lec19 - Modules and Random Numbers.pptx
PDF
Simple C programs
PPTX
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
PDF
Acm aleppo cpc training fifth session
PPT
44 randomized-algorithms
PPTX
CPP Homework Help
PDF
Acm aleppo cpc training second session
PDF
A01
PPTX
Project in programming
lecture56.ppt
lecture56functionsggggggggggggggggggg.ppt
Random numbers c++ class 11 and 12
C++ Homework Help
Function in cpu 1
Chp7_C++_Functions_Part1_Built-in functions.pptx
First game using c language
FP 201 - Unit 3 Part 2
54602399 c-examples-51-to-108-programe-ee01083101
C chap05
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
CMSC 201 - Lec19 - Modules and Random Numbers.pptx
Simple C programs
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
Acm aleppo cpc training fifth session
44 randomized-algorithms
CPP Homework Help
Acm aleppo cpc training second session
A01
Project in programming

Recently uploaded (20)

PPTX
master seminar digital applications in india
PPTX
Cell Types and Its function , kingdom of life
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
RMMM.pdf make it easy to upload and study
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Insiders guide to clinical Medicine.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Lesson notes of climatology university.
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Pre independence Education in Inndia.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
master seminar digital applications in india
Cell Types and Its function , kingdom of life
Renaissance Architecture: A Journey from Faith to Humanism
Module 4: Burden of Disease Tutorial Slides S2 2025
Sports Quiz easy sports quiz sports quiz
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
RMMM.pdf make it easy to upload and study
Supply Chain Operations Speaking Notes -ICLT Program
Insiders guide to clinical Medicine.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Complications of Minimal Access Surgery at WLH
Lesson notes of climatology university.
VCE English Exam - Section C Student Revision Booklet
Pharma ospi slides which help in ospi learning
Final Presentation General Medicine 03-08-2024.pptx
Pre independence Education in Inndia.pdf
human mycosis Human fungal infections are called human mycosis..pptx

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