# Random Weapon Stats (please read before posting)

**URL:** <https://forum.kirupa.com/t/random-weapon-stats-please-read-before-posting/184156>\
**Category:** programming\
**Created:** [April 2, 2006, 7:36pm UTC](https://forum.kirupa.com/t/random-weapon-stats-please-read-before-posting/184156 "2006-04-02T19:36:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nich](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@Nich](https://forum.kirupa.com/u/Nich)\
**Post date:** [April 2, 2006, 7:36pm UTC](https://forum.kirupa.com/t/random-weapon-stats-please-read-before-posting/184156/1 "2006-04-02T19:36:24Z")

</div>

Before you post, please note that I know how to generate random stats using Math.random().

What I need is a formula, using Math.random, that creates a lower chance of getting higher numbers. I’ve thought about this, and I’m positive there is a simple solution, but I can’t think of one that doesn’t involve doing Math.random\*100 and then using if’s to assign the damages.

If you know of a simple formula which will have a better chance to output small numbers then high using math.random please answer.

---

<div class="post-metadata">

**Author:** ![bombsledder](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bombsledder](https://forum.kirupa.com/u/bombsledder)\
**Post date:** [April 2, 2006, 8:46pm UTC](https://forum.kirupa.com/t/random-weapon-stats-please-read-before-posting/184156/2 "2006-04-02T20:46:35Z")

</div>

you mean like a critical chance of getting something?

i would say something like this

```auto

function generateRandomNumber(){
    var n=Math.random()*100
    var r
   if(n>85){
       r=Math.random()*100
    }else{
      r=Math.random()*50
   }
return r
}

```

now with this code, just because you have a chance of getting it does not mean that you will get a higher number it just means that you will have a chance, if this is what you want

---

<div class="post-metadata">

**Author:** ![Nich](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@Nich](https://forum.kirupa.com/u/Nich)\
**Post date:** [April 2, 2006, 9:11pm UTC](https://forum.kirupa.com/t/random-weapon-stats-please-read-before-posting/184156/3 "2006-04-02T21:11:31Z")

</div>

after playing around I came up with this: Math.round(((Math.random()\*30)/((Math.random()\*20)+1))+5);

lots of brackets, I know, I’m sorry.  
The first number is the max damage (-5), and the second number is the limiter. 30 is nearly impossible to get, 20’s are rare, 10’s are uncommon. The most common number seems to be 7 (2+5). This looks like it might work. The idea behind it is that the generator has to generate a 30 (1 in 30 shot) then a 0(1 in 20 shot) to output 35. That’s 1 in every 600 which is good enough for me 🙂

---

<div class="post-metadata">

**Author:** ![bombsledder](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bombsledder](https://forum.kirupa.com/u/bombsledder)\
**Post date:** [April 2, 2006, 10:02pm UTC](https://forum.kirupa.com/t/random-weapon-stats-please-read-before-posting/184156/4 "2006-04-02T22:02:06Z")

</div>

sure if thats the way you wan’t to do it, i prefer functinos better though easier on the eyes
