Catalog
  1. 1. True Random
  2. 2. Pseudo-Random
Random

True Random

Random is defined as anything unpredictable, In the field of computer science is still considered an unsolved problem.

“The generation of true random numbers remains and unsolved problem in computer Science.”

Amy Farrah Fowler

But why is it and unsolved problem? Well the problem lies down in the nature of a Machine or Computer, since it’s a deterministic device it is fundamentally impossible for it to generate truly random numbers, although the task seams easier said than done.

“Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin.”

John von Neumman

Pseudo-Random

It is here where Pseudo-random algorithms make their play, although Pseudo-Random number generators only appear random, but are definitely not, sometimes this is good enough depending on the application. Since these series of tutorials aim to build the infamous “Snake” game with a twist, Pseudo-random is good enough for us.

Let’s create a program that generates Pseudo-random coordinates and Pseudo-random types of food for our snake to feast on.

First we will need to import the dedicated library for this purpose in Python, oddly enough called random.

import random

We will build an object that contains a the necessary attributes for the task we need to overcome, in this case a x-coordinate, a y-coordinate, and a type. At the same time we will be assigning a pseudo-random number to each attribute, we need to take into account the limits of each attribute, since my window is (60,20), there is a limit of 60 for the x-coordinate and a limit of 20 for the y-coordinate, we will also give the snack a 0 or a 1 to denote a (0) bad treat or a (1) good treat, but we will use probability to alter this pseudo-random number. We will give an 80% chance of a good treat appearing and a 20% chance of a bad one, this way the game can be fun and can be completed in a couple of minutes.

class Food:
def __init__(self):
self.x_coordinate = random.randint(0,59)
self.y_coordinate = random.randint(0,19)
type = random.randint(0,99)
if type <=19 :
self.type_food = 0 #0 == bad food (*)
else:
self.type_food = 1 #1 == good food (+)

We will build a method to demonstrate that the number generated are useful to us, we will build a print method, that prints the current object created. This method is still part of our Food class.

def print(self):
print('x-coordinate: ', self.x_coordinate)
print('y-coordinate: ', self.y_coordinate)
if self.type_food==1:
print('food: good')
else:
print('food: bad')

Finally to try our program we will build a for statement to generate and print 10 objects with pseudo-random numbers generated and check whether we achieved the goal we were after.

for x in range(0, 10):
food = Food()
food.print()

Check this project on GitHub.

Author: Dennis Masaya
Link: http://dennismasaya.com/2019/08/08/Random/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • PayPal

Comment