如何将 Math.random() 方法生成的当前值与以前的值进行比较

How to compare Math.random() method generated current value with previous value

本文关键字:比较 Math random 方法      更新时间:2023-09-26

我是编程新手,最近开始学习JavaScript。我做了一个小程序,随机生成报价。目前,该程序使用 Math.random(( 方法从数组中获取引号。但有时随机生成的数字与以前相同,报价不会改变。

我想要的是,一旦生成报价,它就不应该再次显示,直到用户看到 7 个不同的报价。我如何通过使用纯JavaScript来实现这一点?(参见 JSFiddle 演示(

如果您知道多种解决方案,请尽可能分享所有解决方案。

这是我的代码:

<body>
    <h1>Random Quotes Generator</h1>
    <p>Click <strong>Generate</strong> button to see quotes.</p>
    <button>Generate!</button>
    <script src="script.js"></script>
</body>
var heading = document.querySelector("h1");
var paragraph = document.querySelector("p");
var button = document.querySelector("button");
var randomQuotes = [
    "I am 1st quote.",
    "I am 2nd quote.",
    "I am 3rd quote.",
    "I am 4th quote.",
    "I am 5th quote",
    "I am 6th quote.",
    "I am 7th quote.",
    "I am 8th quote.",
    "I am 9th quote.",
    "I am 10th quote."
];
var generateQuote = function() {
   paragraph.innerHTML = randomQuotes[Math.floor(Math.random() * 10)];
}
button.onclick = generateQuote;

最短的方法是从数组中拼接选定的项目并将其推送到末尾。随机应介于 0 和 2 之间。这将从选择中保存最后 7 个项目。

var paragraph = document.querySelector("p"),
    button = document.querySelector("button"),
    randomQuotes = ["I am 1st quote.", "I am 2nd quote.", "I am 3rd quote.", "I am 4th quote.", "I am 5th quote.", "I am 6th quote.", "I am 7th quote.", "I am 8th quote.", "I am 9th quote.", "I am 10th quote."],
    generateQuote = function () {
        var quote = randomQuotes.splice(Math.random() * 3 | 0, 1)[0];
        randomQuotes.push(quote);
        paragraph.innerHTML = quote;
    }
button.onclick = generateQuote;
<button>Generate!</button>
<p></p>

我会保存一个包含 7 个先前引号的列表,并确保当前引号不在该列表中。 更新的代码:

var heading = document.querySelector("h1");
var paragraph = document.querySelector("p");
var button = document.querySelector("button");
var randomQuotes = [
    "I am 1st quote.",
    "I am 2nd quote.",
    "I am 3rd quote.",
    "I am 4th quote.",
    "I am 5th quote.",
    "I am 6th quote.",
    "I am 7th quote.",
    "I am 8th quote.",
    "I am 9th quote.",
    "I am 10th quote."
];
var previousQuotes = [];
var generateQuote = function() {
    var quoteNumber = Math.floor(Math.random() * 10);
    if (previousQuotes.length > 6) {
        previousQuotes.shift();
    }
    while (previousQuotes.indexOf(quoteNumber) != -1) {
  	    quoteNumber = Math.floor(Math.random() * 10);
    }
    previousQuotes.push(quoteNumber);
    paragraph.innerHTML = randomQuotes[quoteNumber];
}
button.onclick = generateQuote;
<body>
    <h1>Random Quotes Generator</h1>
    <p>Click <strong>Generate</strong> button to see quotes.</p>
    <button>Generate!</button>
    <script src="script.js"></script>
</body>

您可以将之前显示的报价保存在数组中,并检查下一个随机生成的数字是否不存在:

var generateQuote = function() {
    var quoteToShow = Math.floor(Math.random() * 10);
    while(last7quotes.indexOf(quoteToShow) > 0){
        quoteToShow = Math.floor(Math.random() * 10);
    }
    last7quotes.push(quoteToShow);
    if(last7quotes.length > 7){
        last7quotes.splice(0,1);
    }
    paragraph.innerHTML = randomQuotes[quoteToShow];    
}

见小提琴