Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random variable function?
#1
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.
Reply
#2
what ranfom variable function should do?
Maybe look at random.choice()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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()
Reply
#4
Thank you guys, appreciate it
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable for the value element in the index function?? Learner1 8 629 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 571 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,276 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 921 Aug-07-2023, 05:58 PM
Last Post: Karp
  Noob here. Random quiz program. Using a while loop function and alot of conditionals. monkeydesu 6 1,381 Sep-07-2022, 02:01 AM
Last Post: kaega2
  Whys is asterisk and random variable necessary in these functions? rrowhe4d 5 1,508 Aug-05-2022, 07:53 AM
Last Post: Gribouillis
  Retrieve variable from function labgoggles 2 1,037 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  Cant transfer a variable onto another function KEIKAS 5 1,878 Feb-09-2022, 10:17 PM
Last Post: deanhystad
  Please explain uncommon way of declaring and using variable [function.variable] esphi 4 2,322 Nov-07-2020, 08:59 AM
Last Post: buran
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,203 Sep-18-2020, 02:50 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020