防止后续随机结果在 Javascript 中重复

发布于 2024-12-23 04:29:07 字数 960 浏览 1 评论 0原文

在我的网站上,我有一个按钮,可以从报价列表中选择随机报价并将随机选择的报价投影到文本框中。这是使用 JavaScript 完成的。

虽然我已经完成了这项工作,但我想要一段额外的代码来防止直接后续的引用与之前的引用相同。我希望以前的任何引用都能够再次出现,只是不直接出现在后面。

如果可能的话,我也希望这样,至少再点击 3 次,所使用的任何报价都不会再次出现 - 但这只是一个额外的好处。

无论如何,我目前拥有的代码如下:

<head>    
  <script language="javascript"><!--
    function GenerateQuote(){var aquote=new Array;
      aquote[0]="\"Quote0\"";
      aquote[1]="\"Quote1\"";
      aquote[2]="\"Quote2\""
      aquote[3]="\"Quote3\"";
      aquote[4]="\"Quote4\"";
      aquote[5]="\"Quote5\"";
      aquote[6]="\"Quote6\"";
    rdmQuote=Math.floor(Math.random()*aquote.length);
    document.getElementById("quoteBox").value=aquote[rdmQuote];
    }
  -->
  </script>
</head>

<body>
  <textarea id="quoteBox" readonly></textarea>
  <button onClick="GenerateQuote()">Entertainment & Hobbies</button>
</body>

提前致谢;我相信这对你们这些聪明人来说不会太难!

On my website I have a button that selects a random quote from a list of quotes and projects the randomly selected quote into a text box. This is done using JavaScript.

Although I have this working, I'd like an additional piece of code that will prevent the directly subsequent quote being the same as the previous. I'd like any quote used to be able appear again however, just not directly following.

If possible I'd also like it so any quote used does not appear again for a minimum of another 3 clicks - but this would just be a bonus.

Anyway the code I currently have is as follows:

<head>    
  <script language="javascript"><!--
    function GenerateQuote(){var aquote=new Array;
      aquote[0]="\"Quote0\"";
      aquote[1]="\"Quote1\"";
      aquote[2]="\"Quote2\""
      aquote[3]="\"Quote3\"";
      aquote[4]="\"Quote4\"";
      aquote[5]="\"Quote5\"";
      aquote[6]="\"Quote6\"";
    rdmQuote=Math.floor(Math.random()*aquote.length);
    document.getElementById("quoteBox").value=aquote[rdmQuote];
    }
  -->
  </script>
</head>

<body>
  <textarea id="quoteBox" readonly></textarea>
  <button onClick="GenerateQuote()">Entertainment & Hobbies</button>
</body>

Thanks in advance; I'm sure it won't be too hard for you brainiacs!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神魇的王 2024-12-30 04:29:07
  1. 填写报价单数组,然后创建一个副本。
  2. 打乱数组的副本(您可以只使用 .sort() 方法,甚至更好,您可以查找 Fisher-Yates 算法的 js 实现
  3. 调用 pop () 在每次点击事件上的数组上,这样每次都会生成不同的报价,直到数组被完全消耗
  4. 时当数组长度为零时转到 1)

参考:

  1. Fill an array of quotation, then create a copy.
  2. Scramble the copy of the array (you can just use .sort() method or even better you can look for a js implementation of Fisher-Yates alghoritm
  3. Call pop() over the array on each click event so you will generate every time a different quote until the array is fully consumed
  4. When length of the array is zero goto 1)

Reference:

寒江雪… 2024-12-30 04:29:07
<head>
    <script type="text/javascript">
!function (){
    var quotes = ["quote0", "quote1", "quote2", "quote3", "quote4", "quote5", "quote6", "quote7", "quote8"],
        shuffleAfter = quotes.length, cur = 0;


    function shuffle( arr ) {
        var l = arr.length, j, i, tmp;

        for( i = l - 1; i > 0; --i ) {
            j = ( Math.random() * ( i + 1 ) ) >>> 0;
            tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }

        return arr;
    }

    function generateQuote(){
        var r;

        if( cur++ % shuffleAfter === 0 ) {
            shuffle(quotes);
        }

        r = quotes.shift();

        quotes.push( r );

        return r;
    }


    window.generateQuote = generateQuote;
}()
    </script>
</head>
<body>
    <textarea id="quoteBox" readonly></textarea>
    <button onClick="document.getElementById('quoteBox').value = generateQuote()">Entertainment & Hobbies</button>
</body>

调用generateQuote() 27次的结果:

2,9,7,5,8,1,3,4,6,9,6,1,7,5,4,3,2,8,3, 1,6,5,2,7,9,4,8​​,2

正如你所看到的,在一个完整的循环之后,报价再次被打乱,并且如果最后一次出现相同的报价,则有可能出现相同的报价最后一个周期,并且是新周期中的第一个。它至少应该比我的 mp3 播放器中的播放列表随机播放要好得多:P

<head>
    <script type="text/javascript">
!function (){
    var quotes = ["quote0", "quote1", "quote2", "quote3", "quote4", "quote5", "quote6", "quote7", "quote8"],
        shuffleAfter = quotes.length, cur = 0;


    function shuffle( arr ) {
        var l = arr.length, j, i, tmp;

        for( i = l - 1; i > 0; --i ) {
            j = ( Math.random() * ( i + 1 ) ) >>> 0;
            tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }

        return arr;
    }

    function generateQuote(){
        var r;

        if( cur++ % shuffleAfter === 0 ) {
            shuffle(quotes);
        }

        r = quotes.shift();

        quotes.push( r );

        return r;
    }


    window.generateQuote = generateQuote;
}()
    </script>
</head>
<body>
    <textarea id="quoteBox" readonly></textarea>
    <button onClick="document.getElementById('quoteBox').value = generateQuote()">Entertainment & Hobbies</button>
</body>

Results from calling generateQuote() 27 times:

2,9,7,5,8,1,3,4,6,9,6,1,7,5,4,3,2,8,3,1,6,5,2,7,9,4,8,2

As you can see, after a full cycle the quotes are shuffled again and there is a chance the same quote will appear if it was last in the last cycle and is first in the new cycle. It should be much better than playlist shuffling in my mp3 player at least :P

迷荒 2024-12-30 04:29:07

使用

var lastQuote=-1;

在函数外部

var rdmQuote=lastQuote;
while (rdmQuote==lastQUote) rdmQuote=Math.floor(Math.random()*aquote.length);
lastQuote=rdmQuote;

,然后在函数内部使用

use

var lastQuote=-1;

outside your function, then

var rdmQuote=lastQuote;
while (rdmQuote==lastQUote) rdmQuote=Math.floor(Math.random()*aquote.length);
lastQuote=rdmQuote;

inside your function

呆° 2024-12-30 04:29:07

您可以调整 dontRepeatUntil 以满足您的需求
当然有更好的方法,但这个应该可行

var latestQuote = []
    , dontRepeatUntil=3
    , rdmQuote = null
    , quotes=[
        "\"Quote0\""
        ,"\"Quote2\""
        ,"\"Quote3\""
        ,"\"Quote4\""
        ,"\"Quote5\""
        ,"\"Quote6\""
    ]
;
function GenerateQuote(){
if(latestQuote.length >= dontRepeatUntil){
    latestQuote = latestQuote.slice(latestQuote.length-dontRepeatUntil+1);
}
    do{
        rdmQuote=Math.floor(Math.random()*quotes.length);
    }while(latestQuote.join(',').match(new RegExp('(^|,)'+rdmQuote+'(,|$)')));
    latestQuote.push(rdmQuote);
    document.getElementById("quoteBox").value=quotes[rdmQuote];
}

you can tweak dontRepeatUntil to sweats your need
There's certainly better way but this one should work

var latestQuote = []
    , dontRepeatUntil=3
    , rdmQuote = null
    , quotes=[
        "\"Quote0\""
        ,"\"Quote2\""
        ,"\"Quote3\""
        ,"\"Quote4\""
        ,"\"Quote5\""
        ,"\"Quote6\""
    ]
;
function GenerateQuote(){
if(latestQuote.length >= dontRepeatUntil){
    latestQuote = latestQuote.slice(latestQuote.length-dontRepeatUntil+1);
}
    do{
        rdmQuote=Math.floor(Math.random()*quotes.length);
    }while(latestQuote.join(',').match(new RegExp('(^|,)'+rdmQuote+'(,|$)')));
    latestQuote.push(rdmQuote);
    document.getElementById("quoteBox").value=quotes[rdmQuote];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文