Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

javascript - How do you shuffle through an array to generate a random number that doesn't repeat?

I'm trying to shuffle to generate a random number from an array and splice, but keep getting undefined for the last element that's left in the array. Here, I can generate random numbers that do not repeat. Below is the code for the same.

    <body>
        <div id="bingo">
            <script>

                let numbers = new Set()
                        .add("B1")
                        .add("B2")
                        .add("B3")
                        .add("B4")
                        .add("B5")
                        .add("B6")
                        .add("B7")
                        .add("B8")
                        .add("B9")
                        .add("B10");

                let called = Array.from(numbers);

                let display = new Array();


                function getRandomNum()
                {
                    function rando()
                    {
                        for (let i = called.length - 1; i > 0; i++) 
                        {
                            const j = Math.floor(Math.random() * called.length);
                            const number = called[i];
                            called[i] = called[j];
                            called[j] = number;

                            if(number) 
                            {
                                called.splice(j, 1);
                            }
                            
                            if(called.length < 0)
                            {
                                return;
                            } else
                                {
                                    return number;
                                }
                        }

                        
                    }

                    if(called.length === 0)
                    {
                        index = "No More Numbers";
                    }else
                        {
                            index = rando();
                                display.push(index);
                        }
                    document.getElementById('bingo').innerHTML = index;
                    
                }


                function show()
                {
                    for(let n = 0; n < display.length; n++)
                    {
                        document.getElementById('reveal').innerHTML += "<br/>" + display[n] + "<br/>";
                    }
                } 



            </script>
        </div>

        <div id="button">

            <button onclick="getRandomNum()">Random Number</button>

        </div>

        <br/>
        <br/>
        <br/>

        <div id="reveal">

            <button onclick="show()">Numbers Called</button>
        </div>

    </body>
</html>

My only issue is that when it gets to the last element of the original array, it generates "undefined" instead of a value.

Need help with fixing that part so that it displays "No more numbers" once all the elements have been randomly generared and removed.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to replace the for loop with this:

for (let i = 0; i < called.length; i++){

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...