0

Suppose I want the variables a_1, a_2, a_3, a_4 to be random numbers from the uniform distribution U(0,100). I could write

import random

a_1 = random.uniform(0,100) 
a_2 = random.uniform(0,100) 
a_3 = random.uniform(0,100)
a_4 = random.uniform(0,100)

But I was wondering if there is a more efficient way to do this. I tried the following,

import random

for i in range(4):
    a_i = random.uniform(1,100)
    print(a_i)

However, this did not work. For example, when I call a_1 later on, it says a_1 is not defined. How can I fix this?

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
Artus
  • 165
  • 1
  • 2
  • 7
  • 2
    It looks like you are trying to do something in the second piece of code like this: https://stackoverflow.com/questions/5036700/how-can-you-dynamically-create-variables-via-a-while-loop If you only need the 4 variables just stick with the first piece of code. – Zack Tarr Jan 11 '18 at 19:54
  • If it weren't for the XY problem going on here, this would be an obvious duplicate. – Beefster Jan 11 '18 at 19:59
  • Possible duplicate of [How can you dynamically create variables via a while loop?](https://stackoverflow.com/questions/5036700/how-can-you-dynamically-create-variables-via-a-while-loop) – Beefster Jan 11 '18 at 19:59
  • Possible duplicate of [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – Zero Piraeus Jan 12 '18 at 01:08
  • [Why you don't want to dynamically create variables](http://stupidpythonideas.blogspot.cl/2013/05/why-you-dont-want-to-dynamically-create.html) – Zero Piraeus Jan 12 '18 at 01:09

3 Answers3

4

Put the random values into a list and then access members by index:

>>> import random
>>> a = [random.uniform(1,100) for _ in range(4)]
>>> a
[71.4615087249735, 19.04308860149112, 40.278774122696014, 69.18947997939686]
>>> a[0]
71.4615087249735
>>> a[2]
40.278774122696014

Unless you have a really, really good reason, defining variables dynamically is kind of hard to justify.

Brian
  • 3,091
  • 16
  • 29
  • @Brain Thanks a lot, I never thought of making a list. However, what is wrong with the code that I wrote, why did it not work? – Artus Jan 11 '18 at 20:10
  • 1
    @Artus because variables don't work dynamically like that. Your code only defined one variable `a_i` which has no relation to the `i` being iterated in the loop. Regardless of how many times you loop, you're still only reassigning to the one variable `a_i`. What you want is a list (essentially an array) like @Brian's answer, so you can loop through the list like `a[i]` – r.ook Jan 11 '18 at 20:47
1

If you do not wish to use a list, you can use the unpack functionality

import random
up, down, strange, charm = [random.uniform(1,100) for _ in range(4)]
Camion
  • 1,264
  • 9
  • 22
  • Rofl... Took me a while to catch that one ;-) – Camion Jan 12 '18 at 01:27
  • @Camion thank you but I'm not sure I understand what up down strange charm means... – Artus Jan 12 '18 at 02:10
  • Those are just example of names for if you want named variables instead of position in a structure (those name are colors of [quarks](https://en.wikipedia.org/wiki/Quark), just for fun). – Camion Jan 12 '18 at 02:25
  • The pack/unpack mechanism allows you to initialize multiple variables in one single operation : (a, b, c) = (1, 2, 3), but in this case the outside parenthesis are not required : a, b, c = 1, 2, 3 – Camion Jan 12 '18 at 02:28
1

it seems You need numbers in range(0,100), well if you wanna generate random integer within a range(or other iterable) try this. just in case you need it to be more specific

import random numberOf_intRequired=4 random_int=random.sample(range(0,100),numberOf_intRequired)