Generating set of semi-random numbers with some guaranteed present
I am attempting to create a Python 3 script which prints out all possible sets of 6 numbers, 4 of which are fixed (must appear), but can appear anywhere in the 6. The numbers also must all be between 0 and 9.
For instance, if the "fixed" numbers are 1,2,3,4
, then 1,2,4,3,5,7
is acceptable, 1,2,3,3,4,4
is also acceptable, but 0,4,3,7,8,2
or 8,0,5,6,7,8
are not and should not be printed.
I have attempted so solve this so far, but have run into a mental roadblock when it comes to conceptually understanding where exactly to get started on this.
So far I have attempted to use random.sample, just displaying if the numbers picked are within the range, but ultimately determined that it would not be suitable and that I am most likely barking up the wrong tree here. (I realize that the below is not optimal, just trying to get a working solution. This is just for my personal use for generating 6 digit arma map coordinates, so long as it works I don't overly care what it looks like)
import random
count = 0
while count < 4:
res = random.sample([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 6)
if [0, 0, 2, 8] in res:
print(res);
count += 1
Any help is greatly appreciated.
python-3.x random
|
show 5 more comments
I am attempting to create a Python 3 script which prints out all possible sets of 6 numbers, 4 of which are fixed (must appear), but can appear anywhere in the 6. The numbers also must all be between 0 and 9.
For instance, if the "fixed" numbers are 1,2,3,4
, then 1,2,4,3,5,7
is acceptable, 1,2,3,3,4,4
is also acceptable, but 0,4,3,7,8,2
or 8,0,5,6,7,8
are not and should not be printed.
I have attempted so solve this so far, but have run into a mental roadblock when it comes to conceptually understanding where exactly to get started on this.
So far I have attempted to use random.sample, just displaying if the numbers picked are within the range, but ultimately determined that it would not be suitable and that I am most likely barking up the wrong tree here. (I realize that the below is not optimal, just trying to get a working solution. This is just for my personal use for generating 6 digit arma map coordinates, so long as it works I don't overly care what it looks like)
import random
count = 0
while count < 4:
res = random.sample([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 6)
if [0, 0, 2, 8] in res:
print(res);
count += 1
Any help is greatly appreciated.
python-3.x random
1
Not sure, if this will work for you but you can consider random.choice?
– Dmytro Chasovskyi
Jan 1 at 8:41
3
How about: fix your 4 numbers, then generate the last 2 using your favourite RNG.
– Hong Ooi
Jan 1 at 8:58
1
Use your favourite shuffle to randomise the order
– Hong Ooi
Jan 1 at 9:16
1
@DmytroChasovskyi Why use acceptance rejection when it can be done quite directly?
– pjs
Jan 1 at 20:51
1
Your question says that you want a script that "prints out all possible sets of 6 numbers [...]". Is that what you want, or do you just want to generate some of the possible sets at random? If you want all of them, it's a bit strange to be using therandom
module.
– Mark Dickinson
Jan 2 at 17:39
|
show 5 more comments
I am attempting to create a Python 3 script which prints out all possible sets of 6 numbers, 4 of which are fixed (must appear), but can appear anywhere in the 6. The numbers also must all be between 0 and 9.
For instance, if the "fixed" numbers are 1,2,3,4
, then 1,2,4,3,5,7
is acceptable, 1,2,3,3,4,4
is also acceptable, but 0,4,3,7,8,2
or 8,0,5,6,7,8
are not and should not be printed.
I have attempted so solve this so far, but have run into a mental roadblock when it comes to conceptually understanding where exactly to get started on this.
So far I have attempted to use random.sample, just displaying if the numbers picked are within the range, but ultimately determined that it would not be suitable and that I am most likely barking up the wrong tree here. (I realize that the below is not optimal, just trying to get a working solution. This is just for my personal use for generating 6 digit arma map coordinates, so long as it works I don't overly care what it looks like)
import random
count = 0
while count < 4:
res = random.sample([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 6)
if [0, 0, 2, 8] in res:
print(res);
count += 1
Any help is greatly appreciated.
python-3.x random
I am attempting to create a Python 3 script which prints out all possible sets of 6 numbers, 4 of which are fixed (must appear), but can appear anywhere in the 6. The numbers also must all be between 0 and 9.
For instance, if the "fixed" numbers are 1,2,3,4
, then 1,2,4,3,5,7
is acceptable, 1,2,3,3,4,4
is also acceptable, but 0,4,3,7,8,2
or 8,0,5,6,7,8
are not and should not be printed.
I have attempted so solve this so far, but have run into a mental roadblock when it comes to conceptually understanding where exactly to get started on this.
So far I have attempted to use random.sample, just displaying if the numbers picked are within the range, but ultimately determined that it would not be suitable and that I am most likely barking up the wrong tree here. (I realize that the below is not optimal, just trying to get a working solution. This is just for my personal use for generating 6 digit arma map coordinates, so long as it works I don't overly care what it looks like)
import random
count = 0
while count < 4:
res = random.sample([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 6)
if [0, 0, 2, 8] in res:
print(res);
count += 1
Any help is greatly appreciated.
python-3.x random
python-3.x random
edited Jan 1 at 17:22


Dmytro Chasovskyi
829828
829828
asked Jan 1 at 8:35
MarkyrosonMarkyroson
347
347
1
Not sure, if this will work for you but you can consider random.choice?
– Dmytro Chasovskyi
Jan 1 at 8:41
3
How about: fix your 4 numbers, then generate the last 2 using your favourite RNG.
– Hong Ooi
Jan 1 at 8:58
1
Use your favourite shuffle to randomise the order
– Hong Ooi
Jan 1 at 9:16
1
@DmytroChasovskyi Why use acceptance rejection when it can be done quite directly?
– pjs
Jan 1 at 20:51
1
Your question says that you want a script that "prints out all possible sets of 6 numbers [...]". Is that what you want, or do you just want to generate some of the possible sets at random? If you want all of them, it's a bit strange to be using therandom
module.
– Mark Dickinson
Jan 2 at 17:39
|
show 5 more comments
1
Not sure, if this will work for you but you can consider random.choice?
– Dmytro Chasovskyi
Jan 1 at 8:41
3
How about: fix your 4 numbers, then generate the last 2 using your favourite RNG.
– Hong Ooi
Jan 1 at 8:58
1
Use your favourite shuffle to randomise the order
– Hong Ooi
Jan 1 at 9:16
1
@DmytroChasovskyi Why use acceptance rejection when it can be done quite directly?
– pjs
Jan 1 at 20:51
1
Your question says that you want a script that "prints out all possible sets of 6 numbers [...]". Is that what you want, or do you just want to generate some of the possible sets at random? If you want all of them, it's a bit strange to be using therandom
module.
– Mark Dickinson
Jan 2 at 17:39
1
1
Not sure, if this will work for you but you can consider random.choice?
– Dmytro Chasovskyi
Jan 1 at 8:41
Not sure, if this will work for you but you can consider random.choice?
– Dmytro Chasovskyi
Jan 1 at 8:41
3
3
How about: fix your 4 numbers, then generate the last 2 using your favourite RNG.
– Hong Ooi
Jan 1 at 8:58
How about: fix your 4 numbers, then generate the last 2 using your favourite RNG.
– Hong Ooi
Jan 1 at 8:58
1
1
Use your favourite shuffle to randomise the order
– Hong Ooi
Jan 1 at 9:16
Use your favourite shuffle to randomise the order
– Hong Ooi
Jan 1 at 9:16
1
1
@DmytroChasovskyi Why use acceptance rejection when it can be done quite directly?
– pjs
Jan 1 at 20:51
@DmytroChasovskyi Why use acceptance rejection when it can be done quite directly?
– pjs
Jan 1 at 20:51
1
1
Your question says that you want a script that "prints out all possible sets of 6 numbers [...]". Is that what you want, or do you just want to generate some of the possible sets at random? If you want all of them, it's a bit strange to be using the
random
module.– Mark Dickinson
Jan 2 at 17:39
Your question says that you want a script that "prints out all possible sets of 6 numbers [...]". Is that what you want, or do you just want to generate some of the possible sets at random? If you want all of them, it's a bit strange to be using the
random
module.– Mark Dickinson
Jan 2 at 17:39
|
show 5 more comments
2 Answers
2
active
oldest
votes
The following takes a specified list of fixed values, determines how many additional random numbers are needed and appends them, then shuffles the list and returns it.
import random
def generate_set(fixed_vals = [1, 2, 3, 4], length = 6, min = 0, max = 9):
for _ in range(length - len(fixed_vals)):
fixed_vals.append(random.randint(min, max))
random.shuffle(fixed_vals)
return fixed_vals
print(generate_set()) # produces, e.g., [0, 3, 4, 8, 2, 1]
clearly, superior answer
– Severin Pappadeux
Jan 7 at 18:51
add a comment |
My solution works out for the sequence that should contain 0, 0, 2, 8
(weights specified for this sequence). The solution could be further generalized.
def generate_sequence(weights):
return random.choices([0,1,2,3,4,5,6,7,8,9], weights=weights, k=6)
def check_sequence(sequence, check):
return check(sequence)
def check():
# TODO: Add your own implementation of sequence checker
pass
# You could play with weights to generate sequence with higher probability
weights = [4,1,2,1,1,1,1,1,2,1]
sequence = generate_sequence(weights)
while check_sequence(sequence, check):
sequence = generate_sequence(weights)
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%2f53994084%2fgenerating-set-of-semi-random-numbers-with-some-guaranteed-present%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
The following takes a specified list of fixed values, determines how many additional random numbers are needed and appends them, then shuffles the list and returns it.
import random
def generate_set(fixed_vals = [1, 2, 3, 4], length = 6, min = 0, max = 9):
for _ in range(length - len(fixed_vals)):
fixed_vals.append(random.randint(min, max))
random.shuffle(fixed_vals)
return fixed_vals
print(generate_set()) # produces, e.g., [0, 3, 4, 8, 2, 1]
clearly, superior answer
– Severin Pappadeux
Jan 7 at 18:51
add a comment |
The following takes a specified list of fixed values, determines how many additional random numbers are needed and appends them, then shuffles the list and returns it.
import random
def generate_set(fixed_vals = [1, 2, 3, 4], length = 6, min = 0, max = 9):
for _ in range(length - len(fixed_vals)):
fixed_vals.append(random.randint(min, max))
random.shuffle(fixed_vals)
return fixed_vals
print(generate_set()) # produces, e.g., [0, 3, 4, 8, 2, 1]
clearly, superior answer
– Severin Pappadeux
Jan 7 at 18:51
add a comment |
The following takes a specified list of fixed values, determines how many additional random numbers are needed and appends them, then shuffles the list and returns it.
import random
def generate_set(fixed_vals = [1, 2, 3, 4], length = 6, min = 0, max = 9):
for _ in range(length - len(fixed_vals)):
fixed_vals.append(random.randint(min, max))
random.shuffle(fixed_vals)
return fixed_vals
print(generate_set()) # produces, e.g., [0, 3, 4, 8, 2, 1]
The following takes a specified list of fixed values, determines how many additional random numbers are needed and appends them, then shuffles the list and returns it.
import random
def generate_set(fixed_vals = [1, 2, 3, 4], length = 6, min = 0, max = 9):
for _ in range(length - len(fixed_vals)):
fixed_vals.append(random.randint(min, max))
random.shuffle(fixed_vals)
return fixed_vals
print(generate_set()) # produces, e.g., [0, 3, 4, 8, 2, 1]
edited Jan 1 at 22:05
answered Jan 1 at 20:47
pjspjs
13.2k41541
13.2k41541
clearly, superior answer
– Severin Pappadeux
Jan 7 at 18:51
add a comment |
clearly, superior answer
– Severin Pappadeux
Jan 7 at 18:51
clearly, superior answer
– Severin Pappadeux
Jan 7 at 18:51
clearly, superior answer
– Severin Pappadeux
Jan 7 at 18:51
add a comment |
My solution works out for the sequence that should contain 0, 0, 2, 8
(weights specified for this sequence). The solution could be further generalized.
def generate_sequence(weights):
return random.choices([0,1,2,3,4,5,6,7,8,9], weights=weights, k=6)
def check_sequence(sequence, check):
return check(sequence)
def check():
# TODO: Add your own implementation of sequence checker
pass
# You could play with weights to generate sequence with higher probability
weights = [4,1,2,1,1,1,1,1,2,1]
sequence = generate_sequence(weights)
while check_sequence(sequence, check):
sequence = generate_sequence(weights)
add a comment |
My solution works out for the sequence that should contain 0, 0, 2, 8
(weights specified for this sequence). The solution could be further generalized.
def generate_sequence(weights):
return random.choices([0,1,2,3,4,5,6,7,8,9], weights=weights, k=6)
def check_sequence(sequence, check):
return check(sequence)
def check():
# TODO: Add your own implementation of sequence checker
pass
# You could play with weights to generate sequence with higher probability
weights = [4,1,2,1,1,1,1,1,2,1]
sequence = generate_sequence(weights)
while check_sequence(sequence, check):
sequence = generate_sequence(weights)
add a comment |
My solution works out for the sequence that should contain 0, 0, 2, 8
(weights specified for this sequence). The solution could be further generalized.
def generate_sequence(weights):
return random.choices([0,1,2,3,4,5,6,7,8,9], weights=weights, k=6)
def check_sequence(sequence, check):
return check(sequence)
def check():
# TODO: Add your own implementation of sequence checker
pass
# You could play with weights to generate sequence with higher probability
weights = [4,1,2,1,1,1,1,1,2,1]
sequence = generate_sequence(weights)
while check_sequence(sequence, check):
sequence = generate_sequence(weights)
My solution works out for the sequence that should contain 0, 0, 2, 8
(weights specified for this sequence). The solution could be further generalized.
def generate_sequence(weights):
return random.choices([0,1,2,3,4,5,6,7,8,9], weights=weights, k=6)
def check_sequence(sequence, check):
return check(sequence)
def check():
# TODO: Add your own implementation of sequence checker
pass
# You could play with weights to generate sequence with higher probability
weights = [4,1,2,1,1,1,1,1,2,1]
sequence = generate_sequence(weights)
while check_sequence(sequence, check):
sequence = generate_sequence(weights)
answered Jan 1 at 9:28


Dmytro ChasovskyiDmytro Chasovskyi
829828
829828
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%2f53994084%2fgenerating-set-of-semi-random-numbers-with-some-guaranteed-present%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
1
Not sure, if this will work for you but you can consider random.choice?
– Dmytro Chasovskyi
Jan 1 at 8:41
3
How about: fix your 4 numbers, then generate the last 2 using your favourite RNG.
– Hong Ooi
Jan 1 at 8:58
1
Use your favourite shuffle to randomise the order
– Hong Ooi
Jan 1 at 9:16
1
@DmytroChasovskyi Why use acceptance rejection when it can be done quite directly?
– pjs
Jan 1 at 20:51
1
Your question says that you want a script that "prints out all possible sets of 6 numbers [...]". Is that what you want, or do you just want to generate some of the possible sets at random? If you want all of them, it's a bit strange to be using the
random
module.– Mark Dickinson
Jan 2 at 17:39