3

I have

    print(randint(0,12))

Is there a quicker easier way of being able to execute this three times in a line other than copying it all out or using

    def RandNum(number):
        print(randint(0,12))

so is there just a way I can run the first piece of code really quickly instead of then going on and using.

    (RandNum("number"))

When ever I need the random number?

Thanks. I want to find as many alternative methods to doing this as possible.

halfer
  • 19,824
  • 17
  • 99
  • 186
Merlindima
  • 48
  • 6

4 Answers4

2

Hope this helps!!

from random import randint

def RandNum(number):
    l = []
    for _ in range(number):
        l.append(randint(0,12))
    print " ".join(map(str, l))

RandNum(3)
Pradeep
  • 1,198
  • 3
  • 12
  • 22
1

So you want the random randint to execute only 3 times. This is how

from random import randint
timesexecuted = int(0)
while timesexecuted < 3:
    print(randint(0,12))
    timesexecuted = timesexecuted + 1

#after this anything you want happen next.

This method is most efficient when you are looping things larger amounts of times, such as, you want to executed print(randint(0,12)) over hundreds of times, this method is probably easiest to write.

Explanation

timesexecuted is set to int(0) since no loops have been run and we need to define timesexecuted. We need to run a loop 3 times so to do that, in the loop, we add on 1 to timesexecuted with timesexecuted + 1 and so once timesexecuted is up to 3 (starts on 0, does 1 loop, is now 1, after another loop, becomes 2, does a third loop, is now 3). And since we have the loop under while timesexecuted < 3:, as timesexecuted is now 3 because the loop has run 3 times, timesexecuted < 3 is no longer true and the loop will run no more. The 3 can also be changed to any number, making this method probably the best if looping many times. (eg. Looping this program 200 times with while timesexecuted < 200:)

Hope I helped.

Luke
  • 439
  • 1
  • 12
  • 26
1

If you are using python 3, you can use the print() function with a list of three numbers. The optional parameter sep has default value of a space, so you don't have to define it if you need three numbers separated by a space.

print(*[random.randint(0, 12) for _ in range(3)])
eumiro
  • 207,213
  • 34
  • 299
  • 261
0

Firstly, is there a reason why you cannot copy out 'randint'? This is the most efficient solution, however does involve some typing:

number1 = random.randint(0,12)

If you don't mind using a function (which I recommend) try setting a variable using the function:

def RandNum():
        return random.randint(0,12)

number1 = RandNum()
number2 = RandNum()

This will make the numbers random, but constant throughout the program. Good luck on your program!

P.S. The return command stops the function and 'returns' to the main program. e.g. if I have the code:

def function():
        return 3
        return 2
        return 1

Only 3 will be returned and used by the main program.

Myleopard
  • 1
  • 3
  • Reason being so is I want to achieve maximum efficiency there are many people better at Python than me on this so why not ask, I personally don't know all of them. – Merlindima Jan 16 '16 at 21:15
  • @Merlindima The most efficient way of doing this is to declare all the variables on the same line, therefore using the first solution and combining the three gives `number1, number2, number3 = random.randint(0,12), random.randint(0,12), random.randint(0,12)` – Myleopard Jan 17 '16 at 23:41