#Generating a weighted random int

6 messages · Page 1 of 1 (latest)

shy cypress
#

I'm trying to generate a random int, weighted towards 1. Ideally it's unbounded, but it's not strictly necessary if that's not feasible or practical.

My current solution has been to have an array of ints with duplicate values weighting the options, then choosing a random int from those options. But I'm certain there's a better option
eg
int[] randOptions = {9, 8, 8, 7, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1};

vocal nicheBOT
#

This post has been reserved for your question.

Hey @shy cypress! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 720 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

wanton nymph
#

idk if there is an objectively "better" option, just an option with different tradeoff

shy cypress
#

I ended up going with

Random r = new Random();
int outcome = (int)Math.ceil((1/r.nextDouble())/2);```