0

My goal is to be able to generate random tuples consisting of 3 variables. 2 be float(x,y coordinates ) and the last one a string. Each tuple should have the format of (float, float, string). I'm pretty sure the x,y is easy but i'm unsure if its possible to generate a string as the third parameter. The string should be a random choice of within a set. For example say I have a list of strings ["one", "two" , "three"]. I would want my generated tuple to consist of two random floats and one of the strings within that set. I was thinking something similar to this code below

[(randint(0, 180), randint(0, 180)) for _ in range(100)]

Once again just to clarify i am just trying to figure out if its possible to add one the strings from the set as my 3rd variable in my tuple

Jeff Pernia
  • 79
  • 1
  • 13

3 Answers3

1

Use random.choice

import random
strings = ['one', 'two', 'three']
[(random.randint(0, 180), random.randint(0, 180), random.choice(strings)) for _ in range(100)]
Corentin Limier
  • 4,946
  • 1
  • 13
  • 24
1
[(randint(0, 180), randint(0, 180)) for _ in range(100)]

This does not generate random floats, it insteads generate random integers as evident by the function name. Instead:

[(uniform(0, 180), uniform(0, 180), choice(["one,"two","three"]) for _ in range(100)]

Note:

random.uniform generates a random float within range(0,180)

random.choice selects a random element from ["one","two","three"]

0

You could get random number for the index of the list of strings and use that to get the random string

list_of_strings = ["one", "two" , "three"]

print [(randint(0, 180), randint(0, 180), list_of_strings[randint(0, len(list_of_strings) -1 )]) for _ in range(100)]
Radan
  • 1,630
  • 5
  • 25
  • 38