# web practice -- card drawing simulator (1)

Posted by rahish on Sat, 28 Dec 2019 16:06:14 +0100

There are so many card pullers playing. I have come to write a card puller simulator. There are six card classes: s s s, ss, s, a, b, c. the probability of sss is 0.01%, ss probability is 0.1%, s probability is 1%, a probability is 15%, b probability is 30%, and the rest is c. in order to avoid staying in Africa for a long time, the setting of probability compensation is added. Every time I draw a card below s, the value of probability compensation counter will rise, The value of this counter directly affects the probability of drawing the card above S. when drawing the card above s, reset this counter to prevent the probability of drawing the advanced card from being too high. At present, it only has the function of single drawing. We will make up ten consecutive drawing next time. The principle of card extraction is to generate an integer value of 0-9999 through Math.floor(Math.random()*10000), and determine the type of card extracted according to the size of the value.

## The code is as follows:

```<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>Draw Card Simulator</title>
<body>
<p id="p1"></p>
<br>
<form>
<input type="button" value="Pumping card" onclick="clickButton()">
</form>
<script>

var sssCount = 0;
var ssCount = 0;
var sCount = 0;
var aCount = 0;
var bCount = 0;
var cCount = 0;
var compensate = 0;
var randomDraw = 0;

function drawCard(){
randomDraw = Math.floor(Math.random() * 10000);
if (randomDraw <= compensate) {
sssCount++;
compensate = 0;
}
else if (randomDraw <= compensate * 6 + 10) {
ssCount++;
compensate = 0;
}
else if (randomDraw <= compensate * 16 + 110) {
sCount++;
compensate = 0;
}
else if (randomDraw <= compensate * 16 + 1610) {
aCount++;
compensate++;
}
else if (randomDraw <= compensate * 16 + 4610) {
bCount++;
compensate++;
}
else {
cCount++;
compensate++;
}

}
function showCard(){
var p = document.getElementById("p1");
var str = "sssCount: " + sssCount + "<br>ssCount: " + ssCount + "<br>sCount: " + sCount + "<br>aCount: " + aCount + "<br>bCount: " + bCount + "<br>cCount: " + cCount;
p.innerHTML = str;
}
function clickButton(){
drawCard();
showCard();
}
</script>
<body>
</html>```

Topics: simulator REST