Selecting One String from data list in Python - python

I am relatively new to programming, so be easy on me.
I have made a program (much like a magic 8 ball) that the user asks a question, and then I have created a list with all my answers inside of it (14 answers). My program shuffles all the answers, but then I now need to assign every piece of data (using a random number generator from 1-14 (which I've completed), and then have it match the shuffled data with a random number and print that.
I have created the random number generator, and the answer list shuffles. I just need to know how to assign the number to the strings in my list, and then print that one string.

Lists have indices; if your number is between 0 and 13 (inclusive), then you can just use that directly on your list:
print(answers[random_number])
However, the random module has a dedicated function for just this use case; random.choice() picks one value from a sequence at random:
print(random.choice(answers))
No need to shuffle anything that way..
Demo:
>>> import random
>>> answers = ['Without a doubt!', 'Hmmm, not so sure', 'By the winds, set sail now!', 'Oh no, no NO NO!!']
>>> print(random.choice(answers))
Hmmm, not so sure
>>> print(random.choice(answers))
Oh no, no NO NO!!

Related

Built-in way to do immutable shuffle in Python (CPython)? [duplicate]

This question already has an answer here:
How to shuffle a copied list without shuffling the original list?
(1 answer)
Closed 4 years ago.
The random.shuffle() built-in shuffles in place, which is fine for many purposes. But suppose we would want to leave the original collection intact, and generate a random permutation based on the original sequence, is there a prefered way to do this in the standard library?
When I look at CPython's random.py I see an intial comment that reads:
sequences
---------
pick random element
pick random sample
pick weighted random sample
generate random permutation
Particularly, the last line is of interest. However, I struggle to see what method in this class achieves this.
Naturally, this is not a hard problem to solve, even for a novice Python programmer. But it would be nice to have a standard way of doing it in the standard library, and I'm sure it must exist somewhere. Perhaps someplace other than random.py?
According to the docs of random.shuffle(), you could use random.sample():
To shuffle an immutable sequence and return a new shuffled list, use sample(x, k=len(x)) instead of shuffle().
The same thing was analized in this post
This seems like the obvious solution that shouldn't do more work than necessary:
def shuffled(gen):
ls = list(gen)
random.shuffle(ls)
return ls
Since it's so simple to build from stdlib primitives, I'm not sure it would make sense to include this as a separate primitive.

I need to generate random numbers but each number must be different from the other

I got a function in another file, to recall that function i use:
app.put_number(row, column, number)
which is basically generating a matrix, but in the first column, for each row I need a random number from 1 to 20, and each number must be unique, I tried using random.randrange().
For example:
app.put_number(0, 0, random.randrange(1,21))
app.put_number(1, 0, random.randrange(1,21))
But sometimes it generates the same number and I need that each one of them to be different.
Plus I cant mess with the file with the actual function, I can only use if, else, while, for and app.put_number(row, column, number)
It would be helpful if you tagged your question with a specific language you would like to complete this task in.
Conceptually, however; you need to check the populated array every time a new number is generated to ensure that you are not inserting a number already in the array. If you want to get fancy you could even have an array of numbers and randomly choose the index of the array you are putting the numbers into, effectively "shuffling" them together.
Try using the Random.sample(). It returns unique numbers in a range
As per doc below "To choose a sample from a range of integers, use a range() object as an argument. This is especially fast and space efficient for sampling from a large population: sample(range(10000000), k=60)."
https://docs.python.org/3/library/random.html
One solution is to keep the generated numbers in a list and then calling the random number generator until it gives a number that is not present in your list of already generated numbers.

lists functions- len ,cal

I'm thinking about making a small program that calculates a mathematical base.
I need to know the length of a list and I want to store in a variable its value!
I also want to add all the lists elements "they are numbers" and store there value all together in a variable.
Note: the list is really long, so I want an easy way. I know other long ways!
numbers=[1,2,3,4,5,6]
x=len(numbers)
print(x)
Now how to all them all?
Just use sum()
numbers=[1,2,3,4,5,6]
x=sum(numbers)
print(x)
>>prints 21

Will changing seed in rnd.randrange prevent identical results? Python 3.3

I am writing a script where I have a list of 7 integers ranging from 66 to 95. I want this list to be pseudo-randomly different each time I call it, but I want to be sure that it never returns the same random arrangement of random numbers.
This is nestled earlier in the file:
rnd = random.Random()
If I set x to 1 in the seed and call the function like so:
rnd.seed(x)
results = [rnd.randrange(66, 96) for i in range(7)]
and then if I call the function over and over, incrementally adding 1 to x in:
rnd.seed(x)
Will I be guaranteed a different set of random arrangements of random numbers until the number of possible iterations loops? When I write this down I feel like the answer is no. And if the answer is no, is there a way to do this? The program will be opened and closed often and if this works I would start it on the seed it last closed on.
There's absolutely no way to guarantee that generating a random list of 7 integers will always be unique, short of checking the newly generated list against a database of all generated lists you've already created. Random is random, and although the chance is slim (1 in 21,870,000,000 to be precise) it will always be possible without some sort of verification of uniqueness.

How can I run a loop against 2 random elements from a list at a time?

Let's say I have a list in python with several strings in it. I do not know the size. How can I run a loop to do an operation on 2 random elements of this string?
What if I wanted to favour a certain subset of the strings in this randomization, to be selected more often, but still make it possible for them to not be chosen?
you need to look into random module. It has for example a random.choice function that lets you select a random element from a sequence or a random.sample that selects given number of samples, it's easy to account for different weights too.
explain better your problem, what operations and what elements you're focusing?
regarding the problem with the elements beeing chosen more often, give each string an "chance multiplier", each comparison you multiply a number between 1 and 10 and the chance multiplyer of the string, if the result is higher than X (say... 5), so it select the string, if not, it searches for another string. this way, strings with higher multipliers will have more chance to be selected

Categories

Resources