2

I'm working on a very simple t2i generator with simple radio button input for creating a prompt (choose an accessory).

Almost everything is working as expected, but I'm not sure if I'm doing it the best way.

I have an accessory list, and can output any of the accessories correctly... but I want NULL (wearing NO accessories) to be included if "Random" is chosen.

I'm sure there is something simple that I'm missing or maybe there's a better way to do this?

Thanks for any help!

top 3 comments
sorted by: hot top new old
[-] Almaumbria@lemmy.world 3 points 1 week ago* (last edited 1 week ago)

Hii, you could try with a coin flip:

else if (racc.checked) {Math.random() > 0.5 ? "and wearing [randAcc]" : ""}

Math.random() is uniformly distributed, so there's roughly a 50% chance that the result is above 0.5, and thus a 50% chance that you get an accessory.

For different odds, play around with adjusting the threshold (0.5). Make it higher to make accessories less likely, decrease it to make it more likely.

EDIT: Oh, right, I forgot -- you can also calculate the threshold based on how many accessories you have. It should go something like this:

Math.random() > 1/accessories.getLength

If you have five elements in accessories, 1/5 gives you 0.2, so the odds would be about 80% of the time you'll get an accessory. So that's another thingy to play around with.

EDIT2: Just correcting my own faulty math :D

[-] Ashenthorn@lemmy.world 1 points 1 week ago

Thanks so much for the reply!

I never would have thought of this solution, and it will probly do the job. ๐Ÿ‘ I was hoping for something a little more portable.. like somehow setting a null value list item, or maybe a better if/else for the list item output.

[-] yamit@lemmy.world 3 points 1 week ago

There are many ways, one simple is to use .replace("and wearing random", ""), so when the random is selected, "wearing random" will be replaced by an empty string "" (nothing). you put it in wAcc in the prompt, like: wAcc.replace("and wearing random", "")

this post was submitted on 04 Feb 2026
2 points (75.0% liked)

Perchance - Create a Random Text Generator

1767 readers
12 users here now

โš„๏ธŽ Perchance

This is a Lemmy Community for perchance.org, a platform for sharing and creating random text generators.

Feel free to ask for help, share your generators, and start friendly discussions at your leisure :)

This community is mainly for discussions between those who are building generators. For discussions about using generators, especially the popular AI ones, the community-led Casual Perchance forum is likely a more appropriate venue.

See this post for the Complete Guide to Posting Here on the Community!

Rules

1. Please follow the Lemmy.World instance rules.

2. Be kind and friendly.

  • Please be kind to others on this community (and also in general), and remember that for many people Perchance is their first experience with coding. We have members for whom English is not their first language, so please be take that into account too :)

3. Be thankful to those who try to help you.

  • If you ask a question and someone has made a effort to help you out, please remember to be thankful! Even if they don't manage to help you solve your problem - remember that they're spending time out of their day to try to help a stranger :)

4. Only post about stuff related to perchance.

  • Please only post about perchance related stuff like generators on it, bugs, and the site.

5. Refrain from requesting Prompts for the AI Tools.

  • We would like to ask to refrain from posting here needing help specifically with prompting/achieving certain results with the AI plugins (text-to-image-plugin and ai-text-plugin) e.g. "What is the good prompt for X?", "How to achieve X with Y generator?"
  • See Perchance AI FAQ for FAQ about the AI tools.
  • You can ask for help with prompting at the 'sister' community Casual Perchance, which is for more casual discussions.
  • We will still be helping/answering questions about the plugins as long as it is related to building generators with them.

6. Search through the Community Before Posting.

  • Please Search through the Community Posts here (and on Reddit) before posting to see if what you will post has similar post/already been posted.

founded 2 years ago
MODERATORS