User manual

17
How are random numbers generated?
Generally you would think that in a program nothing happens at random. How then is it possible that a
program can generate random numbers? If you divide a large prime number by any value you will get
umpteenth decimal digits that are barely predictable. These decimals change without any regularity, if the
divisor is increased regularly. However, the outcome, which seems ostensibly at random, can be
reproduced with an identical program or multiple calls of the same program at any time. If you are taking
now any figure from these grouped figures and divide it by a number that is the result of the current time
second or the contents of any storage position on the computer, an outcome will be produced that cannot
be reproduced and it is therefore called a random number.
guess = 0 The variable guess will later contain the number guessed by the player. It starts with 0.
i = 0 The variable i is commonly used among programmers as a counter for program loop cycles. Here it is
utilized to count the number of guesses needed by the user to guess the secret number. Also this variable
starts with
0.
while guess != number: The word while initiates a program loop, which in our case is repeated as long as
guess, the number guessed by the user, is unequal the secret number number. Python uses the character
combination
!= to express unequal. Following the colon is the actual program loop.
guess = input(“Your guess:") The function input writes the text Your guess: and waits for the input,
which is then stored in the variable
guess.
Indentations are important in Python
In most programming languages the program loops or decisions are indented to make the code easier to
read. In Python these indents serve not only for better clarity but are an absolutely necessity for the
programming logic. However, specific punctuation is not needed to end a loop or decision.
if number < guess: If the secret number is less than the number guessed by the player guess, then ...
print “The number guessed is less than “,guess
... this text is the output. The variable guess is written here at the end, so that the guessed number will be
displayed in the text. If this condition is not true, the indented line is simply skipped.
if guess < number: If the secret number is greater than the number guessed by the player guess, then ...
print “The number guessed is greater than “,guess
... a different text is displayed.