Home > other >  Python: How to use random to get two numbers in different orders
Python: How to use random to get two numbers in different orders

Time:08-09

I am looking for a fast way to get 2 integers randomly say 1, 2 or 2, 1.

    var1 = random.randint( 1, 2 )
    # do something
    var2 = random.???       # say if var1 == 1, var2 should be 2.
    # do something else

Is there something that can be done to achieve following (Some other syntactical way)

    var2 = ( 1, 2 ) - var1

CodePudding user response:

Many solutions are possible, but the one which matches your idea most would be to use a compact if else statement:

import random

var1 = random.randint(1, 2)
var2 = 2 if var1 == 1 else 1

Another easy way to go is to create a list of values and shuffle them:

import random

l = list(range(1, 3))
random.shuffle(l)
var1 = l[0]
var2 = l[1]

Or use an in build function of random directly sovling your issue:

import random

var1, var2 = random.sample([1, 2], k=2)

CodePudding user response:

I guess you're looking for random.sample:

Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.

import random
first, second = random.sample([1, 2], k=2)
print(first, second)

CodePudding user response:

If you should mix the inputs, you could use

a, b = random.sample([1, 2], k=2) # Where k would be count of elements

If you ok that there could be 2 same outputs

a, b = random.choices([1, 2], k=2)  # a == b possible

CodePudding user response:

You asked for "a fast way", so here's one 10 times faster than the other solutions:

from random import random

var1 = 1 if getrandbits(1) else 2
var2 = 2 if var1 == 1 else 1

Benchmark results including two even faster but longer ways:

  62 ns ± 0 ns  if getrandbits(1):
                    var1 = 1
                    var2 = 2
                else:
                    var1 = 2
                    var2 = 1

  65 ns ± 0 ns  if getrandbits(1):
                    var1, var2 = 1, 2
                else:
                    var1, var2 = 2, 1

  82 ns ± 0 ns  var1 = 1 if getrandbits(1) else 2
                var2 = 2 if var1 == 1 else 1

  88 ns ± 2 ns  var1 = 1 if random_() < 0.5 else 2
                var2 = 2 if var1 == 1 else 1

 841 ns ± 3 ns  var1 = randint(1, 2)
                var2 = 2 if var1 == 1 else 1

 868 ns ± 4 ns  var1 = random.randint(1, 2)
                var2 = 1   2 - var1

 875 ns ± 2 ns  var1 = random.randint(1, 2)
                var2 = 2 if var1 == 1 else 1

1430 ns ± 4 ns  l = list(range(1, 3))
                random.shuffle(l)
                var1 = l[0]
                var2 = l[1]

2400 ns ± 4 ns  var1, var2 = random.sample([1, 2], k=2)

Benchmark code (Try it online!):

from timeit import repeat
from statistics import median, stdev
from random import shuffle

setup = '''
import random
from random import randint, sample, random as random_, getrandbits
'''
codes = '''
var1 = random.randint(1, 2)
var2 = 2 if var1 == 1 else 1

var1 = randint(1, 2)
var2 = 2 if var1 == 1 else 1

l = list(range(1, 3))
random.shuffle(l)
var1 = l[0]
var2 = l[1]

var1, var2 = random.sample([1, 2], k=2)

var1 = 1 if random_() < 0.5 else 2
var2 = 2 if var1 == 1 else 1

var1 = 1 if getrandbits(1) else 2
var2 = 2 if var1 == 1 else 1

if getrandbits(1):
    var1 = 1
    var2 = 2
else:
    var1 = 2
    var2 = 1

if getrandbits(1):
    var1, var2 = 1, 2
else:
    var1, var2 = 2, 1

var1 = random.randint(1, 2)
var2 = 1   2 - var1
'''.strip().split('\n\n')

times = {c: [] for c in codes}
def stats(code):
    ts = [t * 1e9 for t in sorted(times[code])[:5]]
    return 'M ns ±  ns ' % (median(ts), stdev(ts))

number = 10000
for _ in range(50):
  for code in codes:
    t = min(repeat(code, setup, number=number)) / number
    times[code].append(t)
for code in sorted(codes, key=stats):
  print(stats(code), code.replace('\n', '\n' ' '*16))
  print()

CodePudding user response:

One approach specific to two integers that would match your original idea closely would be to subtract the chosen first integer from the sum of the two integers:

var1 = random.randint(1, 2)
var2 = 1   2 - var1
  • Related