Ensure randomized ratio is consistent

Right now I’m doing something 50-50 and I’d like the ratio to be randomized, from ~2/5 to ~3/5, but it–has–to keep that ratio; I know that if I skewed a plain randomizer that the vast majority of the time it would stay close to that, but sometimes a side would end up vastly favored and I want to avoid that. My first thought was creating a boolean array and the pointer would rotate them, the random factor being how many of which (true/false) is put in that array. I have the feeling that wouldn’t be the best way to ensure the ratio remains consistent.

I know that subtractions are done with overflow but that would be for large numbers, I need just a small # like 5, maybe 10 (or perhaps 16 so its hexadecimal). If I could manage that, I could just multiply the random result to that number (as a delimiter) then keep adding to that # and when it becomes smaller again, then the 1st condition would come back (2nd when larger).

The language I’m looking to implement this is ECMAScript, but the principle seem like it could be applied to multiple languages, hence why I posted in General Web Dev (feel free to move this if it wasn’t appropriate).

Thank you kindly for your help

O…kay…

So… you want a weighted table.

It’s simple enough… generate a random `number` between 0 and 1.
Set `total` to 0.
Foreach Weight:
`total += weight`
If `number` <= `total` , the result is that weight’s value. Return it.
EndForeach.

As long as your weights sum to 1, this will always return a value.

I don’t need help in creating a table, I could do that on my own (although how you put it might be better than what I have in mind, unfortunately I don’t understand your explanation). I’m hoping there’s a simpler way (than a table) to ensure I don’t end up with too much on one side than the other no matter the return size, so it needs to be consistent (ie can’t be < ~1/4 neither > ~3/4).

Well you cant have something that is random and consistent in the same breath, so I think you need to give a bit more of what you’re actually trying to do.

If I have 10 things, and I want to distribute them between two boxes such that 1/4 are in one box and 3/4 are in another, first of all I cant because 4 doesnt divide 10 evenly. So no matter what i do, I wont get exactly 1/4 and 3/4, because i’d have to split one of my things in half.

If what you’re trying to do is to divide a given set of results into boxes such that they fall within the right ratios, and those ratios make sense for the number of results that you have… take the array of results, shuffle it, and then split it as you need to. Voila, ‘random’ and yet split.

I kept putting ‘~’ around my ratios because I’m not stuck on them, although 3/4 very near the maximum I’d accept.

I think I’ll just run a boolean randomizer once, have a counter, and a boolean that will cycle each # and when the counter reaches 2, the random will be used then the counter will reset, ending up with 2/5-3/5.

You can’t have “< ~” and expect a definitive response because it’s a subjective measure. What’s < ~1/4? 1/5? 3/16? 7/32? Where’s the limit?

I still have no idea what you’re trying to do, what you’re working with, or what you actually want to come out of the thing. But if you’ve got a working solution, then i’d say run with that.

7/10 is the hard limit, I don’t know why it’s so important, isn’t it “3/4 very near the maximum”. I have not coded what I proposed in post #5 yet. I mentioned in the 1st sentence “I’m doing something 50-50”, is that really important? If it absolutely is, it’s choosing the character case.

Okay, so you’re got a string, and you want to make a certain percentage of the characters uppercase; you dont want a random number of them to be uppercase, you want a fixed percentage, randomly distributed. A full problem description goes a long way towards getting a solution.

How would I go about it. Well, I know how many characters are in the string. (You may want to do some manipulation there - remove the number of non-letter characters, for example).

Take the number, multiply by the weight of the uppercase value. Round the result (because you cant have parts of a character - see above). You now know how many characters need to be uppercase. We’ll call that `X`.

Lowercase the entire string.
Pick `X` unique random integers between 0 and Length - 1.
Set the character at each random integer to its uppercase variant. (Additional manipulation here might be necessary to account for spaces and such again)