Coding Curriculum - Python

Random Number Generators

Now that we finally use the functions, we can discuss what they do. As you might have guessed, they return a random number or item.

The randint(x, y) function, takes 2 parameters. Both parameters represent a range. So the number will return an integer that is greater than or equal to x and smaller than or equal to y. Also since the function is called randint, it will always return an integer. For example:

print(randint(0, 1))   # Will either print 0 or 1
print(randint(0, 100)) # Will print a number between 0 or 100
x = 1
y = 5
print(randint(x, y))   # prints a random number between 1 and 5

The other function that we imported, choice does not actually return a random number. Instead it chooses a random object from an array. So for instance:

x = [1, 2, 3, 4, 5]
print(choice(x))       # will print a random number that is in x
letters = ['a', 'b', 'c', 'd']  
print(choice(letters)) # prints either a, b, c or d

That is all we need to create a word search puzzle. The next steps that you need to take are:

  • Decide if a word is placed horizontally/vertically
  • Find a random column/row to place the first letter of the word
  • See if the word fits
  • Loop until you find an appropriate spot
  • Repeat for the other words