mankand007
mankand007

Reputation: 992

Is there an alternative for the random module in Python?

I was using Trypython.org, and found that there was no random module included, so got curious.

Is there an alternative method to generate random numbers within a certain range without importing random?

Upvotes: 3

Views: 6048

Answers (5)

Aditya99
Aditya99

Reputation: 1

Use "secrets" module- It is a built in module: import secrets secrets.random(10) IT GENERATES A NUMBER BETWEEN 0 TO 10 OR VALUE BY YOU!

Upvotes: 0

Johan Lundberg
Johan Lundberg

Reputation: 27028

Random number generation does not have to be hard. What's hard is inventing your own method and finding a proof that it's good, or writing an optimized/secure/thread safe/.../ implementation.

Here's my Python version of the Multiply-with-carry method as described at http://en.wikipedia.org/wiki/Random_number_generation#Computational_methods

You will get quite good and uniform random numbers between 0 and 1 by calling getuniform(), then just scale them to the size you need. This should be fine for most cases except cryptography or detailed Monte Carlo simulations.

class myrandom:
    kz=36969
    kw=18000
    k3=65535
    maxz=kz*k3+(2<<16)
    maxw=kw*k3+(2<<16)
    max=(maxz<<16 )+maxw
    # Optionally initiate with different seed. Two numbers below 2<<16
    def __init__(self,z=123456789,w=98764321):
        self.m_w = w
        self.m_z = z
    def step(self):
        self.m_z = self.kz * (self.m_z & self.k3) + (self.m_z >> 16)  
        self.m_w = self.kw * (self.m_w & self.k3) + (self.m_w >> 16)       
    def get(self):
        self.step()
        return (self.m_z << 16) + self.m_w
    def time_reseed(self):
        # yes, sure, move out import if you like to
        import time
        t=int(time.time())
        # completely made up way to got two new numbers below 2<<16
        self.m_z = (self.m_z+(t*34567891011)) & ((2<<16)-1)
        self.m_w = (self.m_w+(t*10987654321)) & ((2<<16)-1)
        self.step()
    def getuniform(self):
        return self.get()*1.0/self.max

Example:

myr=myrandom()
print [myr.getuniform() for x in range(20)]

if you call time_reseed() some bits of new randomness from time() is added to the state.

Upvotes: 2

Pep_8_Guardiola
Pep_8_Guardiola

Reputation: 5252

Randomness is not a trivial subject, so instead of an alternative module, I suggest an alternative online IDE instead.

Take a look at pythonfiddle. Here is the code you should use there:

import random
print random.randint(1, 100)

Upvotes: 2

Zashas
Zashas

Reputation: 766

It's quite ugly, unreliable, and not really fast, but it works :

>>> import time
>>> t = time.time()
>>> int(str(t-int(t))[2:])%100 #Keeping only the numbers after the decimal point, otherwise you would get the same "random" number each second
33

The range here is [0,99] (cf the modulo 100). Use it at your own risk.

Upvotes: 2

Max Spencer
Max Spencer

Reputation: 1719

Generating random, or rather pseudo-random numbers is tricky business and it's best to stick to the default libraries. I would recommend you bite the bullet and download Python 2.7.2 or 3.2.3 and then you can play around with any other libraries not included by trypython as well.

Upvotes: 2

Related Questions