View Single Post
Old 01-24-2013, 12:52 AM   PM User | #7
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Arrow

Quote:
Originally Posted by Old Pedant View Post
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".
I think you missed one little '?' in your last example.
Code:
<script type="text/javascript">
var rarityNum = Math.floor( 1 + Math.random() * 100 );
var rarity = ( rarityNum > 95 ) ? "rare"
           : ( rarityNum > 65 ) ? "uncommon"
           : "common";
alert(rarity)
</script>
Looks like it's not only my lighting fast phalanges that can get away from me.
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
Old Pedant (01-24-2013)