-1

I'm having trouble figuring out how to randomly generate strings.

I currently work with import random before creating a set of lists before choiceint = random.randint(1,X). Beyond that I am stuck.

What I want the code to do is select strings from two lists and then create a new string.

For example list1 = ["bannana flavour"] list2 = ["milk"]

and then it would get "bannana flavour" and "milk" and create "bannana flavoured milk".

This would pick from a larger pool to generate the string but I'm clueless as to how generate semi-random strings.

I don't know if this is achievable but the idea is to generate an item from the options in a list and then place it in another list.

If this isn't possible, or isn't possible without a large amount of non user friendly code, I would like to know as, while it is unfavourable, I do have the alternative of using a random.randint(1,X) and then list out all the possible combinations random.randint = 1:

petezurich
  • 9,280
  • 9
  • 43
  • 57
MrBones
  • 1
  • 3
  • 2
    Possible duplicate of [Random strings in Python](https://stackoverflow.com/questions/2030053/random-strings-in-python) – Alexander Kleinhans Apr 04 '18 at 07:12
  • 1
    This isn't at all clear as written. You have two single-element lists, and I can't guess whether you're real problem is picking randomly out of each (presumably larger) list, or picking two out of (presumably more than two) lists, or something else. And I have no idea what the `X` is in `random.randint(1,X)` that you mention a few times. And what does "semi-random" meant? Please read the [mcve] section in the help and try to provide an example that actually demonstrates your problem. – abarnert Apr 04 '18 at 07:17
  • "X" represents a random integer, I didn't think specifying a defined number necessary. and by semi random I mean I don't want it to randomly select characters to use, its semi-random because if it was truly random it would have a literally limitless number of outputs rather than the stricter defined ones I want. – MrBones Apr 04 '18 at 12:42
  • grammar, spelling – petezurich Apr 04 '18 at 15:21

1 Answers1

0

Random has methods to get 1 or more things from a list, with/without duplicates:

To get all combinations between two lists you do not need any randomness, you can use a list comprehension to get all possibilities. If you have two lists of dozends of options it is not wise to generate all combinations, just draw one from each list and return that single combination as result - needs less space to store it.

import random 

def getAllPossibleWins(to,co):
    """Create all combinations of t and c as list of strings"""
    return [f'{c} {t}' for t in to for c in co] 

def getWin():
    """Returns a string with a randomized win."""
    toys = ['cow','hare','car','plane']  # 1000 options
    cols = ['red','green','blue']        # 1000 options

    # instead of creating 1000000 combinations and draw once from it, draw from 1000 twice
    return f'You win a {random.choice(cols)} {random.choice(toys)}.'

print(getWin())
print(getWin())
print(getWin())

print(getAllPossibleWins(toys,cols))

Output:

You win a blue plane. 
You win a red car. 
You win a green cow.

['red cow', 'green cow', 'blue cow', 'red hare', 'green hare', 'blue hare', 'red car', 
 'green car', 'blue car', 'red plane', 'green plane', 'blue plane']
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69