Python Forum
random variable function? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: random variable function? (/thread-21642.html)



random variable function? - Novice_fanatic - Oct-08-2019

Hi,

I was wondering if there is a 'random variable function' in the data base somewhere and if so, what is the name for it?

I've used the 'random integer function' which is great, which was located in 'from random import randint' (with randint being the functions name).

thank you.


RE: random variable function? - buran - Oct-08-2019

what ranfom variable function should do?
Maybe look at random.choice()


RE: random variable function? - Aurthor_King_of_the_Brittons - Oct-08-2019

The following will randomly select from a predetermined list of your choosing. I'm not sure if that helps.

def main():

    
    import random
    varList = ['ab','bb','cb']
    ranVariable = random.sample(varList, 1)
    print(ranVariable)

main()



RE: random variable function? - Novice_fanatic - Oct-08-2019

Thank you guys, appreciate it


RE: random variable function? - jefsummers - Oct-08-2019

Not sure what random variable function means - the typical float between 0 and 1 as in other languages?
Rather than guess - here is a tip for seeing what functions may be available in a particular module
import random
dir(random)
Getting a dir (directory) on an imported module lists the functions
Output:
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_BuiltinMethodType', '_MethodType', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_inst', '_itertools', '_log', '_os', '_pi', '_random', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']
Jumping to the items that don't have an underline at the beginning, you see choice that was mentioned earlier, but also random (which is the 0-1 function), gauss, lognormvariate, and a wide range of random distributions to pull from. Very cool. So, you now want to know what randrange does.
help(random.randrange)
Output:
Help on method randrange in module random: randrange(start, stop=None, step=1, _int=<class 'int'>) method of random.Random instance Choose a random item from range(start, stop[, step]). This fixes the problem with randint() which includes the endpoint; in Python this is usually not what you want.
Probably you want random,
Output:
>>> help(random.random) Help on built-in function random: random(...) method of random.Random instance random() -> x in the interval [0, 1).
But it is good to know there are lots of alternatives and the built in dir and help functions will give you direction.