0

I want to create a variable called Random that is constantly changing every time I call on it.

I want this bit of code:

Random = randint(0, 10)
print Random
print Random
print Random

not to give me something like:

6
6
6

but rather something like:

3
6
1

Without having to reassign Random before I use it every time. Also I used Random inside several different functions. So how do I allow it to be used everywhere in the code without having to reassign it as a random variable every single time?

Sorry if this is a stupid question but I'm very new at this and I have no idea what I'm doing. Please keep the explanations simple.

edit: here's some more code:

def something1(Random, useless, whatever)

etc...

def something2(Random, useless, whatever)

etc...

...

main():

   for index in range(800):

     Random = randint(1, 10)

     something1(Random, useless, whatever)


Random = randing(1, 10)


something2(Random, useless, whatever)

and this goes on for a while.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
Felis Vulpes
  • 33
  • 2
  • 7

6 Answers6

1

If what you say is really what you want (i.e. a variable that gives a random value every time it's used) then you don't really want a variable. Just replace every instance of Random with randint(0, 10). Alternatively, you could make your own function:

def rand10():
    return randint(0, 10)

And then just call rand10() every time you would use your Random variable.

Rose Kunkel
  • 3,102
  • 2
  • 27
  • 53
  • I'm using a function that I need to take a random variable as a parameter. For some reason if I try to call the rand10 function in the parameter it goes: UnboundLocalError: local variable 'Random' referenced before assignment – Felis Vulpes Oct 14 '12 at 02:12
  • userDefinedFunction is a function that I made that accepts 3 parameters. I need that function to loop so I set it inside a for loop: for index in range(800): userDefinedFunction(Random, x, y) – Felis Vulpes Oct 14 '12 at 02:15
  • 2
    The idea is that, rather than using `userDefinedFunction(Random, x, y)`, you would use `userDefinedFunction(rand10(), x, y)`. – Rose Kunkel Oct 14 '12 at 02:34
  • I'm probably going to have to do that then. – Felis Vulpes Oct 14 '12 at 02:39
0

Random is just an integer type that contains the value generated by randint(). Because it is an integer type, it cannot change its value with each call without being reassigned.

What you could do is define a class so that when it is told to print, it returns a different value:

from random import randint

class Random:
   def __str__(self):
      return str(randint(1,10))

random = Random()
print(random)   # prints a value
print(random)   # could print a different value
print(random)   # could print a different value
  • But how do I allow all the functions to use Random without reassigning it with each use of a function? – Felis Vulpes Oct 14 '12 at 02:19
  • @FelisVulpes: Perhaps the better question to ask is, why is it so important to *not* assign it with each use of a function? (If I even understand what you are asking here) – Nick Bastin Oct 14 '12 at 02:26
  • You actually are not reassigning anything in this example. All you are doing is generating a value and printing it whenever print is called on the random object. Would you rather the random object store the value for randint() and then change it only at certain times? – Matthew Miling Oct 14 '12 at 02:27
  • Yes, but that's getting way too over-complicated. I'm just going to use what kunkelwe said. Put the randomint(1, 10) in the parameters. – Felis Vulpes Oct 14 '12 at 02:41
0

In the land of Really-Bad-Ideas, you can do almost what you want, but... Since we're suggesting a really bad idea here, I won't actually write out the code, but I will tell you how to do it and if you want to, you can figure it out...

  1. Create a new module, lets call it Util
  2. Write a class in Util, we'll call that class Crazy
  3. In the class Util.Crazy, create a property called Random, whose getter returns a random value (with whatever bounds you have proscribed)
  4. Make an instance of Util.Crazy called C at module level
  5. Now you can spell it C.Random any time you want a new value

I didn't give it a lot of thought, but I didn't see any obvious way to get rid of C.Random and just call it Random (if you assign Random to Crazy().Random, it just sticks with the value, not the binding). I'm sure there's something truly hideous you could do with globals(), or possibly you could use the raw descriptor protocol to do what you want.

Community
  • 1
  • 1
Nick Bastin
  • 30,415
  • 7
  • 59
  • 78
  • No idea what you just said. I'm a beginner like I said. – Felis Vulpes Oct 14 '12 at 02:23
  • 2
    @FelisVulpes: You should listen to the other people who are telling you that you simply *should not do* what you want to do. Writing a function and calling it is the proper solution here. – Nick Bastin Oct 14 '12 at 02:24
  • I didn't know that was proper practice or something. The program just looks sloppy with me having to reassign the variable every 5th line. – Felis Vulpes Oct 14 '12 at 02:26
  • @FelisVulpes: Hrm, perhaps you should show us some more code - it's hard to be clear what you're talking about. The example you give above could simply be solved by adding parentheses to each line (e.g. calling a function) - `print Random()` – Nick Bastin Oct 14 '12 at 02:28
  • I'm doing this for class and I can't have my teacher finding my code if she googles it. Here's something: def something1(Random, useless, whatever) etc... def something2(Random, useless, whatever) etc... ... main(): for index in range(800): Random = randint(1, 10) something1(Random, useless, whatever) Random = randing(1, 10) something2(Random, useless, whatever) and this goes on for a while. – Felis Vulpes Oct 14 '12 at 02:32
0
import numpy as np
x = "np.random.randn()"
for i in range(3):
   print(eval(x))

eval() evaluates what's written in the str variable passed to it. Every time one calls eval(x) one gets a new random variable.

A bit of a cheating, I agree, but keeps the code a tad cleaner since eval(x) certainly looks better than calling some expression (especially if your rv distribution is defined by a intricate expression and you use it in your code quite often).

0

hello there this might be late but you could try and assign random to none and then give the program a loop using the while function and then there you can assign it to a random number range you desire. I will give an example;

import random
Random = None
while Random != 0:
     Random = random.randint(0,10)
     print(Random)

this means values will keep getting generated as long as they are not zero and if zero is generated the program breaks out of the loop

-1
import random
def numbergeneration():
    number = (random.randrange(0,101))
    print(number)
    return number

I believe this is what you need.

  • 2
    This is a really old question and your answer does not really add any value considering the other answers. Also note the bad formatting of your code. – Romain Vincent Jan 16 '21 at 13:34