0

I am trying to write a program in python 2.7 that has to choose more than one random variable and print it out. But the variable can't be the same as any of the previously printed out variables.

I have been looking around Google and this site, and I have yet to find anything for strings (I have only found integers so far). Here is an example:

sentences = [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p]
print (random.choice(sentences)) + (random.choice(sentences)) + (random.choice(sentences))

>>> a + b + a

I don't want there to be duplicates, I want it to be like this:

>>> a + b + c

Is there any way this can be achieved?

PerfectlyNormal
  • 4,182
  • 2
  • 35
  • 47
CodyJHeiser
  • 67
  • 2
  • 2
  • 9

6 Answers6

13

you can use random.sample()

random.sample(population, k)

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

In [13]: "+".join(random.sample(sentences,3))
Out[13]: 'a+b+c'
fferri
  • 18,285
  • 5
  • 46
  • 95
Ajay
  • 5,267
  • 2
  • 23
  • 30
3

A random value that isn't the same as previous values isn't very random.

Perhaps you'd like to use random.shuffle to just rearrange your list of items randomly, and then you can take one off at a time?

Jay Kominek
  • 8,674
  • 1
  • 34
  • 51
1

May be you want random.sample

>>>mylist = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>print(sum(random.sample(mylist, 3)))
13

OR

>>>"+".join(random.sample(map(str, mylist),3)) #if string map(str,) can avoid
'6+1+3'
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
1
import random

def generate_random(my_list, number_of_choices):
    chosen = []
    cnt = 0
    while cnt < number_of_choices:
        choice = random.choice(my_list)
        if choice not in chosen:
            chosen.append(choice)
            cnt +=1
    return chosen
Todd Minehardt
  • 276
  • 11
  • 17
  • 1
    For my case with a very large list, this approach is better than other ones that want to shuffle the whole list. Easier to pick 1, then pick another and throw it away if it matches the first one. – pauljohn32 Jan 18 '18 at 23:46
  • 1
    I think there is an indentation error here. "cnt += 1" needs to be indented to only happen within the if statement. Otherwise, the loop terminates prematurely – pauljohn32 Mar 31 '18 at 18:37
  • @pauljohn32 - Thank you for the catch. Fixed. – Todd Minehardt Mar 31 '18 at 20:19
0
import random
import string

def sample_n(arr, N):
    return random.sample(arr, N)

sentences = list(string.ascii_lowercase)

print "".join(sample_n(sentences, 3))

I think it would be nice to explain how to implement a sample function without using a designated API function:

>>> arr = range(10)
>>> arr
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> out = [arr.pop(random.randint(0,len(arr))) for i in xrange(3)]
>>> out
[0, 1, 8]
0x90
  • 39,472
  • 36
  • 165
  • 245
-6

I would do like this

#!/usr/bin/python
# -*- coding: utf-8 -*-

import random
sentences = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']

random.shuffle(sentences)

n=3 # amount of items to display

out=''
for item in sentences:
    out=out+ item + ' + '
    n -=1
    if n==0:
        break


print out[:-2]

Output 1

o + p + g

Output 2

b + a + j

Output 3

f + c + i
Alex Ivanov
  • 695
  • 4
  • 6
  • 3
    you could achieve the same thing by replacing the `for` loop with `out = ' + '.join(sentences[:n])` – fferri Jun 17 '15 at 17:08
  • Yes, I could but I won't. I don't like that. – Alex Ivanov Jun 17 '15 at 17:09
  • 5
    mescalinum is right, the foor loop is bad form, bad style, and results in an incorrect string that has to be chopped off when printing. That would be a great improvement, even though using `random.sample` is still superior. – krethika Jun 17 '15 at 17:15