10
CBSE, Grade 12 Computer Science Random Numbers - Notes Malathi Senthil, [email protected] 34 5 7 1 6 9 56 10 22

CBSE, Grade12, Computer Science, Random Numbers - Notes

Embed Size (px)

DESCRIPTION

CBSE Preparatory notes for Grade 12. Topic: "Random Numbers"

Citation preview

Page 1: CBSE, Grade12, Computer Science, Random Numbers - Notes

CBSE, Grade 12Computer ScienceRandom Numbers - Notes

Malathi Senthil,

[email protected]

34 5 7

1 6 8

9

9 561022

Page 2: CBSE, Grade12, Computer Science, Random Numbers - Notes

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

Page 3: CBSE, Grade12, Computer Science, Random Numbers - Notes

2. How to generate a random number?

int r = random(4);

Header file required – stdlib.h

Random(n) generates any random number from 0 to n-1 Example # 1

int r = random(0);

Possible values for r are:

3

2

1

0

Example # 2

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)

Page 4: CBSE, Grade12, Computer Science, Random Numbers - Notes

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();

}

Page 5: CBSE, Grade12, Computer Science, Random Numbers - Notes

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.

Page 6: CBSE, Grade12, Computer Science, Random Numbers - Notes

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

Page 7: CBSE, Grade12, Computer Science, Random Numbers - Notes

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();

}

Page 8: CBSE, Grade12, Computer Science, Random Numbers - Notes

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();

}

Page 9: CBSE, Grade12, Computer Science, Random Numbers - Notes

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#

Page 10: CBSE, Grade12, Computer Science, Random Numbers - Notes

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

34 5 7

1 6 8

9

9 561022