<p>Math.random()で乱数を生成して一致したら停止。<button id="on">START</button> <button id="off">STOP</button></p>
<p>#乱数1: <input id="rand1" type="text" size="18"></p>
<p>#乱数2: <input id="rand2" type="text" size="18"></p>
<p style="color:#080;">TRY: <input id="try" type="text" size="16" value="0">(試行回数)</p>
<p style="color:#f00;">HIT: <input id="hit" type="text" size="16" value="0">(一致した回数)</p>
input{
  color:inherit;
  text-align: right;
  padding:0.25em 0.5em;
}
p{
  margin:0 0 0.5em;
  font-size:12px;
}
var t = 0;
var h = 0;
var timer;
$('#on').click(function() {
  startTimer(timer);
});
$('#off').click(function() {
  clearInterval(timer);
});

function startTimer() {
    timer = setInterval(function() {
    t++;
    var r1 = Math.random();
    var r2 = Math.random();
    $('#rand1').val(r1);
    $('#rand2').val(r2);
    $('#try').val(t);
    if (r1 == r2) {
      h++;
      $('#hit').val(h);
    }
  }, 1);
}

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js