Generating set of semi-random numbers with some guaranteed present












0















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.










share|improve this question




















  • 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


















0















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.










share|improve this question




















  • 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
















0












0








0


0






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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 the random module.

    – Mark Dickinson
    Jan 2 at 17:39
















  • 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










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














2 Answers
2






active

oldest

votes


















3














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]





share|improve this answer


























  • clearly, superior answer

    – Severin Pappadeux
    Jan 7 at 18:51



















0














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)





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%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









    3














    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]





    share|improve this answer


























    • clearly, superior answer

      – Severin Pappadeux
      Jan 7 at 18:51
















    3














    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]





    share|improve this answer


























    • clearly, superior answer

      – Severin Pappadeux
      Jan 7 at 18:51














    3












    3








    3







    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]





    share|improve this answer















    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]






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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



















    • 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













    0














    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)





    share|improve this answer




























      0














      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)





      share|improve this answer


























        0












        0








        0







        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)





        share|improve this answer













        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)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 9:28









        Dmytro ChasovskyiDmytro Chasovskyi

        829828




        829828






























            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%2f53994084%2fgenerating-set-of-semi-random-numbers-with-some-guaranteed-present%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