LightReader

Chapter 20 - Chapter 20 — The Random Generator

The computer screen pulsed softly.

The system was almost fully unlocked.

The cursor blinked slowly.

_

Pran leaned closer to the monitor.

The robot was standing beside the system panel again.

[^_^]

/| |\

/ \

A new system message appeared.

PYTHON MODULE PROGRESS: 90%

NEXT MODULE: RANDOM GENERATOR

Pran smiled.

"Random generator… that sounds interesting."

The computer displayed another message.

SOME PROGRAMS REQUIRE UNPREDICTABLE RESULTS

Pran thought about it for a moment.

Many programs use randomness.

For example:

games that generate random enemies

apps that shuffle music

simulations that create unpredictable events

security systems that generate random codes

Computers normally follow instructions very precisely.

But sometimes programmers want the computer to behave unpredictably.

For that, Python uses a special tool called a module.

The system displayed a new line of code.

import random

Pran read it aloud.

"Import random."

The robot displayed an explanation.

IMPORT LOADS EXTRA TOOLS INTO THE PROGRAM

Python has many built-in modules that provide useful features.

The random module allows programs to generate random numbers.

The computer showed the next example.

import random

number = random.randint(1,10)

print(number)

Pran studied the line carefully.

random.randint(1,10)

The robot explained.

RANDINT MEANS RANDOM INTEGER

So this command generates a random whole number between 1 and 10.

Pran ran the program.

The output appeared.

7

He ran it again.

This time the output was different.

2

He ran it again.

9

Pran smiled.

"So the number changes every time."

Exactly.

That is the power of randomness.

Programs can now create unpredictable behavior.

The computer displayed another example.

A simple guessing game.

import random

secret = random.randint(1,10)

guess = int(input("Guess the number: "))

if guess == secret:

print("Correct!")

else:

print("Wrong! The number was", secret)

Pran typed the program into the editor.

Then he ran it.

The computer asked:

Guess the number:

Pran typed:

5

The screen responded.

Wrong! The number was 8

Pran laughed.

"Okay… that's actually fun."

The robot jumped excitedly.

[^O^]

/| |\

/ \

The computer displayed another message.

RANDOM GENERATOR VERIFIED

Then another system message appeared.

PYTHON MODULE PROGRESS: 95%

Pran leaned back in his chair.

"Only five percent left."

The system flickered again.

Another message appeared.

FINAL PROGRAM MODULE APPROACHING

Pran nodded slowly.

"So everything we learned is about to come together."

He reviewed what he had learned in Python so far:

• printing text• receiving input• numbers and calculations• decision making• loops• lists• functions• processing data• random numbers

All of these tools could now be combined into a complete program.

The robot displayed the next message.

FINAL CHALLENGE INITIALIZING

The cursor blinked again.

_

Waiting.

The final lesson was about to begin.

Pran placed his hands on the keyboard again.

"Alright," he said quietly.

"Let's build something real."

More Chapters