In LUA get a list of random numbers between a range, but allow x set of duplicates per number
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
add a comment |
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
add a comment |
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
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
lua
edited Nov 20 '18 at 4:30
aJynks
asked Nov 20 '18 at 4:23
aJynksaJynks
3001619
3001619
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can use "merge trick":
Create "unical" array of numbers for 5 (10/number of dublicats) elements: 1,2,5,7,9
Repeate #1
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.
add a comment |
- 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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
You can use "merge trick":
Create "unical" array of numbers for 5 (10/number of dublicats) elements: 1,2,5,7,9
Repeate #1
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.
add a comment |
You can use "merge trick":
Create "unical" array of numbers for 5 (10/number of dublicats) elements: 1,2,5,7,9
Repeate #1
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.
add a comment |
You can use "merge trick":
Create "unical" array of numbers for 5 (10/number of dublicats) elements: 1,2,5,7,9
Repeate #1
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.
You can use "merge trick":
Create "unical" array of numbers for 5 (10/number of dublicats) elements: 1,2,5,7,9
Repeate #1
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.
answered Nov 20 '18 at 6:46
A. DenisA. Denis
32012
32012
add a comment |
add a comment |
- 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.
add a comment |
- 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.
add a comment |
- 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.
- 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.
answered Nov 20 '18 at 21:40
aJynksaJynks
3001619
3001619
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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