Reputation: 11
So I want there to be a lower chance that the randomly generated number is between 0 to 20 or between 75 to 90 but a higher chance that the number is between 20 and 75 current randomizing code.
I have tried numerous types of things but I can't seem to figure it out.
The math for generating the number
function doDamage(attacker, reciever, channel, attack) {
let healthbothusers = 100
const healing = testing === false ? Math.floor(healthbothusers = Math.random() * 80) : 100
const damage = testing === false ? Math.floor(healthbothusers = Math.random() * 90) : 100
let recieverHealth = usersHealth.get(reciever)
let attackerHealth = usersHealth.get(attacker)
let newHealth = recieverHealth - damage
let beenhealed = attackerHealth + healing
This is my first ever stack post so I hope it's done properly ;)
Upvotes: 0
Views: 712
Reputation: 359
Do Math.random
twice - first time to determine which block of results you want (the wings, or the more common central range), then do it again to work out the random result from within that block.
Therefore, you can control how frequently the wings come up. This halves the chances of each wing coming up, compared to natural distribution:
let r=Math.random()
if (r<0.1) { // Giving 0-20 only a 10% chance of occurring
r=Math.floor(Math.random() * 20)
} else if (r>0.92){ // Giving 75-90 only a 8% chance of occurring
r=75+Math.floor(Math.random() * 15)
} else { //Giving central range the remainder chance (82%)
r=20+Math.floor(Math.random() * 55) //Range goes from 20 to 75
}
Upvotes: 1
Reputation: 10617
Math.floor(Math.random()*100)+1
should give you percents between 1 and 100. As long as that random percent is > 100-yourPercent
it should be approximately the correct percent of the time. Check this out:
//<![CDATA[
/* js/external.js */
let doc, htm, bod, nav, M, I, mobile, S, Q, Hopeful; // for reuse on other loads
addEventListener('load', ()=>{
doc = document; htm = doc.documentElement; bod = doc.body; nav = navigator; M = tag=>doc.createElement(tag); I = id=>doc.getElementById(id);
mobile = nav.userAgent.match(/Mobi/i) ? true : false;
S = (selector, within)=>{
let w = within || doc;
return w.querySelector(selector);
}
Q = (selector, within)=>{
let w = within || doc;
return w.querySelectorAll(selector);
}
Hopeful = function(percent){
this.percent = percent;
this.chanceIt = ()=>{
const p = this.percent;
return Math.floor(Math.random()*100)+1 > 100-this.percent;
}
}
// small Library above for reuse - magic below can be put on another page using a load Event *(except // end load line and below)*
const fifty = new Hopeful(50), twentyFive = new Hopeful(25), one = new Hopeful(1);
const seventyFive = new Hopeful(75), oneHundred = new Hopeful(100), zero = new Hopeful(0);
function test(){ // approximates showing proof of concept
let zeroN = 0, fiftyN = 0, twentyFiveN = 0, oneN = 0, seventyFiveN = 0, oneHundredN = 0;
for(let i=0,l=100; i<l; i++){
if(zero.chanceIt())zeroN++;
if(one.chanceIt())oneN++;
if(twentyFive.chanceIt())twentyFiveN++;
if(fifty.chanceIt())fiftyN++;
if(seventyFive.chanceIt())seventyFiveN++;
if(oneHundred.chanceIt())oneHundredN++;
}
console.clear();
console.log('zero', zeroN);
console.log('one', oneN);
console.log('twentyFive', twentyFiveN);
console.log('fifty', fiftyN);
console.log('seventyFive', seventyFiveN);
console.log('oneHundred', oneHundredN);
}
const chance = I('chance');
test(); chance.onclick = test;
}); // end load
//]]>
/* css/external.css */
*{ /* font size may affect white space line breaks - set individually */
box-sizing:border-box; font:0; padding:0; margin:0;
}
html,body,.full{
width:100%; height:100%;
}
.full{
background:#ccc; padding:7px;
}
button{
cursor:pointer; width:100%; background:linear-gradient(#1b7bbb,#147); color:#fff; font:bold 28px Tahoma, Geneva, sans-serif; padding:5px 10px; border:1px solid #007; border-radius:10px;
}
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8' /><meta name='viewport' content='width=device-width, height=device-height, initial-scale:1, user-scalable=no' />
<title>Title Here</title>
<link type='text/css' rel='stylesheet' href='css/external.css' />
<script src='js/external.js'></script>
</head>
<body>
<div class='full'>
<button id='chance'>chance it</button>
</div>
</body>
</html>
This design allow you to give a percent chance to anything.
Upvotes: 0