How to generate random lists with no duplicate members? [duplicate]









up vote
0
down vote

favorite













This question already has an answer here:



  • How do I create a list of random numbers without duplicates?

    12 answers



a=
from random import randint as rd
for i in range(42):
a.append(rd(1,42))
j=0
k=len(a)-1
while(j<=k):
while(a[i]==a[j]):
a[i]=rd(1,42)
j=0
j=j+1


I cannot figure out how to generate entirely unique random lists in python. All the members have to be unique and cannot by any means be the same as any other member of the list










share|improve this question















marked as duplicate by jpp python-3.x
Users with the  python-3.x badge can single-handedly close python-3.x questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 10 at 4:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















    up vote
    0
    down vote

    favorite













    This question already has an answer here:



    • How do I create a list of random numbers without duplicates?

      12 answers



    a=
    from random import randint as rd
    for i in range(42):
    a.append(rd(1,42))
    j=0
    k=len(a)-1
    while(j<=k):
    while(a[i]==a[j]):
    a[i]=rd(1,42)
    j=0
    j=j+1


    I cannot figure out how to generate entirely unique random lists in python. All the members have to be unique and cannot by any means be the same as any other member of the list










    share|improve this question















    marked as duplicate by jpp python-3.x
    Users with the  python-3.x badge can single-handedly close python-3.x questions as duplicates and reopen them as needed.

    StackExchange.ready(function()
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function()
    $hover.showInfoMessage('',
    messageElement: $msg.clone().show(),
    transient: false,
    position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
    dismissable: false,
    relativeToBody: true
    );
    ,
    function()
    StackExchange.helpers.removeMessages();

    );
    );
    );
    Nov 10 at 4:56


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite












      This question already has an answer here:



      • How do I create a list of random numbers without duplicates?

        12 answers



      a=
      from random import randint as rd
      for i in range(42):
      a.append(rd(1,42))
      j=0
      k=len(a)-1
      while(j<=k):
      while(a[i]==a[j]):
      a[i]=rd(1,42)
      j=0
      j=j+1


      I cannot figure out how to generate entirely unique random lists in python. All the members have to be unique and cannot by any means be the same as any other member of the list










      share|improve this question
















      This question already has an answer here:



      • How do I create a list of random numbers without duplicates?

        12 answers



      a=
      from random import randint as rd
      for i in range(42):
      a.append(rd(1,42))
      j=0
      k=len(a)-1
      while(j<=k):
      while(a[i]==a[j]):
      a[i]=rd(1,42)
      j=0
      j=j+1


      I cannot figure out how to generate entirely unique random lists in python. All the members have to be unique and cannot by any means be the same as any other member of the list





      This question already has an answer here:



      • How do I create a list of random numbers without duplicates?

        12 answers







      python python-3.x






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 4:44









      Nick Parsons

      3,0832619




      3,0832619










      asked Nov 10 at 4:39









      Mirza Mohammad Azwad

      1




      1




      marked as duplicate by jpp python-3.x
      Users with the  python-3.x badge can single-handedly close python-3.x questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Nov 10 at 4:56


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by jpp python-3.x
      Users with the  python-3.x badge can single-handedly close python-3.x questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Nov 10 at 4:56


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote













          Try this:



          import random
          random.sample(range(100), 10)


          In [1715]: random.sample(range(100), 10)
          Out[1715]: [66, 48, 97, 5, 39, 67, 78, 96, 71, 19]

          In [1716]: random.sample(range(100), 10)
          Out[1716]: [50, 89, 60, 69, 81, 86, 27, 94, 4, 62]


          This will generate a list of 10 random numbers from the range 0 to 99, without duplicates.






          share|improve this answer




















          • @MirzMohammadAzwad Please accept the answer if it helped you. This lets the community know that you are not looking for anymore answers.
            – Mayank Porwal
            Nov 11 at 19:20

















          up vote
          0
          down vote













          Use shuffle in that case:



          from random import shuffle

          random_numbers = list(range(1, 43))
          shuffle(random_numbers)


          Or, with randint as you tried,



          from random import randint as rd

          random_numbers =
          for i in range(42):
          r = rd(1, 42)
          while r in random_numbers:
          r = rd(1, 42)

          random_numbers.append(r)





          share|improve this answer





























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote













            Try this:



            import random
            random.sample(range(100), 10)


            In [1715]: random.sample(range(100), 10)
            Out[1715]: [66, 48, 97, 5, 39, 67, 78, 96, 71, 19]

            In [1716]: random.sample(range(100), 10)
            Out[1716]: [50, 89, 60, 69, 81, 86, 27, 94, 4, 62]


            This will generate a list of 10 random numbers from the range 0 to 99, without duplicates.






            share|improve this answer




















            • @MirzMohammadAzwad Please accept the answer if it helped you. This lets the community know that you are not looking for anymore answers.
              – Mayank Porwal
              Nov 11 at 19:20














            up vote
            4
            down vote













            Try this:



            import random
            random.sample(range(100), 10)


            In [1715]: random.sample(range(100), 10)
            Out[1715]: [66, 48, 97, 5, 39, 67, 78, 96, 71, 19]

            In [1716]: random.sample(range(100), 10)
            Out[1716]: [50, 89, 60, 69, 81, 86, 27, 94, 4, 62]


            This will generate a list of 10 random numbers from the range 0 to 99, without duplicates.






            share|improve this answer




















            • @MirzMohammadAzwad Please accept the answer if it helped you. This lets the community know that you are not looking for anymore answers.
              – Mayank Porwal
              Nov 11 at 19:20












            up vote
            4
            down vote










            up vote
            4
            down vote









            Try this:



            import random
            random.sample(range(100), 10)


            In [1715]: random.sample(range(100), 10)
            Out[1715]: [66, 48, 97, 5, 39, 67, 78, 96, 71, 19]

            In [1716]: random.sample(range(100), 10)
            Out[1716]: [50, 89, 60, 69, 81, 86, 27, 94, 4, 62]


            This will generate a list of 10 random numbers from the range 0 to 99, without duplicates.






            share|improve this answer












            Try this:



            import random
            random.sample(range(100), 10)


            In [1715]: random.sample(range(100), 10)
            Out[1715]: [66, 48, 97, 5, 39, 67, 78, 96, 71, 19]

            In [1716]: random.sample(range(100), 10)
            Out[1716]: [50, 89, 60, 69, 81, 86, 27, 94, 4, 62]


            This will generate a list of 10 random numbers from the range 0 to 99, without duplicates.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 10 at 4:41









            Mayank Porwal

            3,1501620




            3,1501620











            • @MirzMohammadAzwad Please accept the answer if it helped you. This lets the community know that you are not looking for anymore answers.
              – Mayank Porwal
              Nov 11 at 19:20
















            • @MirzMohammadAzwad Please accept the answer if it helped you. This lets the community know that you are not looking for anymore answers.
              – Mayank Porwal
              Nov 11 at 19:20















            @MirzMohammadAzwad Please accept the answer if it helped you. This lets the community know that you are not looking for anymore answers.
            – Mayank Porwal
            Nov 11 at 19:20




            @MirzMohammadAzwad Please accept the answer if it helped you. This lets the community know that you are not looking for anymore answers.
            – Mayank Porwal
            Nov 11 at 19:20












            up vote
            0
            down vote













            Use shuffle in that case:



            from random import shuffle

            random_numbers = list(range(1, 43))
            shuffle(random_numbers)


            Or, with randint as you tried,



            from random import randint as rd

            random_numbers =
            for i in range(42):
            r = rd(1, 42)
            while r in random_numbers:
            r = rd(1, 42)

            random_numbers.append(r)





            share|improve this answer


























              up vote
              0
              down vote













              Use shuffle in that case:



              from random import shuffle

              random_numbers = list(range(1, 43))
              shuffle(random_numbers)


              Or, with randint as you tried,



              from random import randint as rd

              random_numbers =
              for i in range(42):
              r = rd(1, 42)
              while r in random_numbers:
              r = rd(1, 42)

              random_numbers.append(r)





              share|improve this answer
























                up vote
                0
                down vote










                up vote
                0
                down vote









                Use shuffle in that case:



                from random import shuffle

                random_numbers = list(range(1, 43))
                shuffle(random_numbers)


                Or, with randint as you tried,



                from random import randint as rd

                random_numbers =
                for i in range(42):
                r = rd(1, 42)
                while r in random_numbers:
                r = rd(1, 42)

                random_numbers.append(r)





                share|improve this answer














                Use shuffle in that case:



                from random import shuffle

                random_numbers = list(range(1, 43))
                shuffle(random_numbers)


                Or, with randint as you tried,



                from random import randint as rd

                random_numbers =
                for i in range(42):
                r = rd(1, 42)
                while r in random_numbers:
                r = rd(1, 42)

                random_numbers.append(r)






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 10 at 5:01

























                answered Nov 10 at 4:56









                JH Jang

                263




                263













                    Popular posts from this blog

                    How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                    Syphilis

                    Darth Vader #20