View Single Post
01-23-2013, 02:35 AM   PM User | #6
Old Pedant
Supreme Master coder!

Join Date: Feb 2009
Posts: 24,940
Thanks: 75
Thanked 4,305 Times in 4,272 Posts
Quote:
 Originally Posted by komichi I want to control the rate of generation via probability. ... is it possible to assign a universal probability of being generated to a group of items?
And I say again: SHOW us some *ACTUAL* examples.

I will show you *one* example:
Code:
```var rarityNum = Math.floor( 1 + Math.random() * 100 );
var rarity;
if ( rarityNum > 95 ) { rarity = "rare"; }
else if ( rarityNum > 65 ) { rarity = "uncommon"; }
else { rarity = "common"; }```
Of you could do it thus:
Code:
```var rarityNum = Math.floor( 1 + Math.random() * 100 );
var rarity = ( rarityNum > 95 ) ? "rare"
: ( rarityNum > 65 ) "uncommon"
: "common";```
Naturally you can control the numbers (here 95 and 65) as you want.

rarityNum, as I coded it, will be a number form 1 to 100.

So also as coded, there is a 5% chance you will get "rare", 30% chance for "uncommon", and 65% chance for "common".
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.