How to use the Random Module in Python


How to use the Random Module in Python

The random module provides access to functions that support many operations.

When to use it?

  • We want the computer to pick a random number in a given range
  • Pick a random element from a list, pick a random card from a deck, flip a coin etc.
  • When making your password database more secure or powering a random page feature of your website.

Random functions

  • Randint
    • If we wanted a random integer, we can use the randint function
    • Randint accepts two parameters: a lowest and a highest number.
    • Generate integers between 1,5. The first value should be less than the second.
      import random
      print random.randint(1, 5)
      
  • Random
    • If you want a larger number, you can multiply it.
      For example, a random number between 0 and 100:
      import random
      random.random() * 100
      
  • Choice
    • Generate a random value from the sequence sequence.
    • random.choice( [‘red’, ‘black’, ‘green’] ).
    • The choice function can often be used for choosing a random element from a list.
      import random
      myList = [2, 109, False, 10, "Lorem", 482, "Ipsum"]
      random.choice(myList)
      
  • Shuffle
    • The shuffle function, shuffles the elements in list in place, so they are in a random order.
    • random.shuffle(list)
    • Example taken from this post on Stackoverflow
      from random import shuffle
      x = [[i] for i in range(10)]
      shuffle(x)
      
  • Randrange
    • Generate a randomly selected element from range(start, stop, step)
    • random.randrange(start, stop[, step])
      import random
      for i in range(3):
      print random.randrange(0, 101, 5)
      

A quick intro to Dependency Injection: what it is, and when to use it

let’s understand what a dependency in programming means.

When class A uses some functionality of class B, then its said that class A has a dependency of class B.

transferring the task of creating the object to someone else and directly using the dependency is called dependency injection.

Why should I use dependency injection?

Let’s say we have a car class which contains various objects such as wheels, engine, etc.

Here the car class is responsible for creating all the dependency objects. Now, what if we decide to ditch MRFWheels in the future and want to use Yokohama Wheels?

We will need to recreate the car object with a new Yokohama dependency. But when using dependency injection (DI), we can change the Wheels at runtime (because dependencies can be injected at runtime rather than at compile time).

You can think of DI as the middleman in our code who does all the work of creating the preferred wheels object and providing it to the Car class.

It makes our Car class independent from creating the objects of Wheels, Battery, etc.

There are basically three types of dependency injection:

  • constructor injection: the dependencies are provided through a class constructor.
  • setter injection: the client exposes a setter method that the injector uses to inject the dependency.
  • interface injection: the dependency provides an injector method that will inject the dependency into any client passed to it. Clients must implement an interface that exposes a setter method that accepts the dependency.

So now its the dependency injection’s responsibility to:

  • Create the objects
  • Know which classes require those objects
  • And provide them all those objects

Benefits of using DI

  1. Helps in Unit testing.
  2. Boiler plate code is reduced, as initializing of dependencies is done by the injector component.
  3. Extending the application becomes easier.
  4. Helps to enable loose coupling, which is important in application programming.

Disadvantages of DI

  1. It’s a bit complex to learn, and if overused can lead to management issues and other problems.
  2. Many compile time errors are pushed to run-time.
  3. Dependency injection frameworks are implemented with reflection or dynamic programming. This can hinder use of IDE automation, such as “find references”, “show call hierarchy” and safe refactoring.