I have a discord bot that I want to have 50% chance to send a chosen sentence and will send a different message if the other one is not chosen (python)
Welcome to the forum ^^
What is your question? Import random, create a number and check it’s value.
I think there’s multiple ways to implement this, but here’s an approach using weights on
random.choices (I think this should do the job)-
import random def choose_sentence(): sentences = ['sentence A', 'sentence B'] return random.choices(sentences, [1, 1])
Alternatively, you could use
random.choice(), which uses normal probability(almost equal chances of all choices) -
import random def choose_sentence(): sentences = ['sentence A', 'sentence B'] return random.choice(sentences)
The result in both the above cases would be same, but random.choices allows you to set relative weights on each list choice, and thus you could use it to have slightly different probability systems, say something like - 40% chance of Heads and 60% chance of Tails
import random coin_flip = random.choices(['HEADS', 'TAILS'], [4, 6])
(The probability of each choice = It’s corresponding weight divided by total sum of weights)
It depends on how your code is set up, but if you’re using
await message.channel.send(), you could call this function in there-
import random def choose_sentence(): sentences = ['sentence A', 'sentence B'] return random.choice(sentences) @client.event async def on_message(message): if message.content.startswith("$hi"): await message.channel.send(choose_sentence())
Where would I put this inside of my code?
hipptiy hoppity your code is now my property
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.