In LUA get a list of random numbers between a range, but allow x set of duplicates per number












1















There is a ton of examples of generating random numbers in LUA that have no duplicates, and just a standard math.random(x,y) can get a set of random whole numbers in a range....



... but I am having trouble finding a set of random numbers between a range, but allowing x amount of duplicates. For my immediate needs I can allow 1 set of duplicates, but it would be great to have code where you can set "duplicate value" to anything for future projects.



Example : I want to generate a list of 10 whole numbers between 1-10... each value can be anything between 1-10, but any one number can only be generated and added to the list twice.



Example Result: 1,1,2,4,5,5,7,7,8,9



In this example result math.random() tried to spit out 3 or more of the same number, but the code makes it go back and try again if it has already produced 2 of the same number.



Thanks in advance!










share|improve this question





























    1















    There is a ton of examples of generating random numbers in LUA that have no duplicates, and just a standard math.random(x,y) can get a set of random whole numbers in a range....



    ... but I am having trouble finding a set of random numbers between a range, but allowing x amount of duplicates. For my immediate needs I can allow 1 set of duplicates, but it would be great to have code where you can set "duplicate value" to anything for future projects.



    Example : I want to generate a list of 10 whole numbers between 1-10... each value can be anything between 1-10, but any one number can only be generated and added to the list twice.



    Example Result: 1,1,2,4,5,5,7,7,8,9



    In this example result math.random() tried to spit out 3 or more of the same number, but the code makes it go back and try again if it has already produced 2 of the same number.



    Thanks in advance!










    share|improve this question



























      1












      1








      1








      There is a ton of examples of generating random numbers in LUA that have no duplicates, and just a standard math.random(x,y) can get a set of random whole numbers in a range....



      ... but I am having trouble finding a set of random numbers between a range, but allowing x amount of duplicates. For my immediate needs I can allow 1 set of duplicates, but it would be great to have code where you can set "duplicate value" to anything for future projects.



      Example : I want to generate a list of 10 whole numbers between 1-10... each value can be anything between 1-10, but any one number can only be generated and added to the list twice.



      Example Result: 1,1,2,4,5,5,7,7,8,9



      In this example result math.random() tried to spit out 3 or more of the same number, but the code makes it go back and try again if it has already produced 2 of the same number.



      Thanks in advance!










      share|improve this question
















      There is a ton of examples of generating random numbers in LUA that have no duplicates, and just a standard math.random(x,y) can get a set of random whole numbers in a range....



      ... but I am having trouble finding a set of random numbers between a range, but allowing x amount of duplicates. For my immediate needs I can allow 1 set of duplicates, but it would be great to have code where you can set "duplicate value" to anything for future projects.



      Example : I want to generate a list of 10 whole numbers between 1-10... each value can be anything between 1-10, but any one number can only be generated and added to the list twice.



      Example Result: 1,1,2,4,5,5,7,7,8,9



      In this example result math.random() tried to spit out 3 or more of the same number, but the code makes it go back and try again if it has already produced 2 of the same number.



      Thanks in advance!







      lua






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 4:30







      aJynks

















      asked Nov 20 '18 at 4:23









      aJynksaJynks

      3001619




      3001619
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You can use "merge trick":




          1. Create "unical" array of numbers for 5 (10/number of dublicats) elements: 1,2,5,7,9


          2. Repeate #1


          3. Merge arrays.



          You can generalize it with paramers of minValue, maxValue, totalNumber, numberOfDublicates, but will need to little more code for handling 10/3 problems and maxValue < totalNumber.






          share|improve this answer































            0















            • Generate a sequential list of non-random numbers between a range with
              no duplicates.

            • Add them to a table, but add each number X amount of times, where X
              is the total amount of duplicates allowed. So we know have a table x
              times as long with each individual number listed X amount of times.

            • Shuffle the table, or generate a list of random numbers or both.

            • Then simply extract the numbers from the table using the generated
              numbers as the numeric key value for the "duplicate" table.

            • You can store anything at those key values so this works for
              anything.. not just numbers.






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386188%2fin-lua-get-a-list-of-random-numbers-between-a-range-but-allow-x-set-of-duplicat%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              You can use "merge trick":




              1. Create "unical" array of numbers for 5 (10/number of dublicats) elements: 1,2,5,7,9


              2. Repeate #1


              3. Merge arrays.



              You can generalize it with paramers of minValue, maxValue, totalNumber, numberOfDublicates, but will need to little more code for handling 10/3 problems and maxValue < totalNumber.






              share|improve this answer




























                1














                You can use "merge trick":




                1. Create "unical" array of numbers for 5 (10/number of dublicats) elements: 1,2,5,7,9


                2. Repeate #1


                3. Merge arrays.



                You can generalize it with paramers of minValue, maxValue, totalNumber, numberOfDublicates, but will need to little more code for handling 10/3 problems and maxValue < totalNumber.






                share|improve this answer


























                  1












                  1








                  1







                  You can use "merge trick":




                  1. Create "unical" array of numbers for 5 (10/number of dublicats) elements: 1,2,5,7,9


                  2. Repeate #1


                  3. Merge arrays.



                  You can generalize it with paramers of minValue, maxValue, totalNumber, numberOfDublicates, but will need to little more code for handling 10/3 problems and maxValue < totalNumber.






                  share|improve this answer













                  You can use "merge trick":




                  1. Create "unical" array of numbers for 5 (10/number of dublicats) elements: 1,2,5,7,9


                  2. Repeate #1


                  3. Merge arrays.



                  You can generalize it with paramers of minValue, maxValue, totalNumber, numberOfDublicates, but will need to little more code for handling 10/3 problems and maxValue < totalNumber.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 6:46









                  A. DenisA. Denis

                  32012




                  32012

























                      0















                      • Generate a sequential list of non-random numbers between a range with
                        no duplicates.

                      • Add them to a table, but add each number X amount of times, where X
                        is the total amount of duplicates allowed. So we know have a table x
                        times as long with each individual number listed X amount of times.

                      • Shuffle the table, or generate a list of random numbers or both.

                      • Then simply extract the numbers from the table using the generated
                        numbers as the numeric key value for the "duplicate" table.

                      • You can store anything at those key values so this works for
                        anything.. not just numbers.






                      share|improve this answer




























                        0















                        • Generate a sequential list of non-random numbers between a range with
                          no duplicates.

                        • Add them to a table, but add each number X amount of times, where X
                          is the total amount of duplicates allowed. So we know have a table x
                          times as long with each individual number listed X amount of times.

                        • Shuffle the table, or generate a list of random numbers or both.

                        • Then simply extract the numbers from the table using the generated
                          numbers as the numeric key value for the "duplicate" table.

                        • You can store anything at those key values so this works for
                          anything.. not just numbers.






                        share|improve this answer


























                          0












                          0








                          0








                          • Generate a sequential list of non-random numbers between a range with
                            no duplicates.

                          • Add them to a table, but add each number X amount of times, where X
                            is the total amount of duplicates allowed. So we know have a table x
                            times as long with each individual number listed X amount of times.

                          • Shuffle the table, or generate a list of random numbers or both.

                          • Then simply extract the numbers from the table using the generated
                            numbers as the numeric key value for the "duplicate" table.

                          • You can store anything at those key values so this works for
                            anything.. not just numbers.






                          share|improve this answer














                          • Generate a sequential list of non-random numbers between a range with
                            no duplicates.

                          • Add them to a table, but add each number X amount of times, where X
                            is the total amount of duplicates allowed. So we know have a table x
                            times as long with each individual number listed X amount of times.

                          • Shuffle the table, or generate a list of random numbers or both.

                          • Then simply extract the numbers from the table using the generated
                            numbers as the numeric key value for the "duplicate" table.

                          • You can store anything at those key values so this works for
                            anything.. not just numbers.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 20 '18 at 21:40









                          aJynksaJynks

                          3001619




                          3001619






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386188%2fin-lua-get-a-list-of-random-numbers-between-a-range-but-allow-x-set-of-duplicat%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              MongoDB - Not Authorized To Execute Command

                              How to fix TextFormField cause rebuild widget in Flutter

                              in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith