Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] - Weighted Dice

Years of Service
User Offline
Joined: 5th Aug 2013
Posted: 5th Aug 2013 23:34
This came after playing a football manager game were a low quality striker was scoring goals for fun. Instead of his shooting attribute (lets say it was 5) been his limit (i.e RND(5), I wondered if it could be made to be his average.

This code lets you specify a dice (lets say 1-20) and allows you to weight it, so it is more likely to land on the specified number, but it can be higher or lower.

The code utilises Green Gandalfs natural log function (found on this forum - saved me a massive headache). I've reposted it here to make the code complete, I hope that OK.

The main function to call is _weighted_dice(mean,stddev,min,max), where:

mean = the value you want to be most likely
stddev = the spread of the values (lower = more likely to hit the mean value)
min = the lowest value allowed
max = the max value allowed

You can also call the _box_muller() function on it's own, but this will return a float, and depending on where you set your figures, can return a negative value. The _weighted_dice() function merely turn this into an int and prevent the numbers falling out of range

I've also attached a picture showing some results. I ran the function 6000 times, counted the numbers rolled and plotted them. The results do look something like a normal distribution, which I was aimimg for. The mean was 10, stdev 3, min 1 and max 20.

I've not tested this to the limit, so if anyone uses it and finds a limitation, I'd be interested to know.




Login to view attachments

Login to post a reply

Server time is: 2023-10-03 01:36:11
Your offset time is: 2023-10-03 01:36:11