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
python python-3.x
marked as duplicate by jpp
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.
add a comment |
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
python python-3.x
marked as duplicate by jpp
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.
add a comment |
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
python python-3.x
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
python python-3.x
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
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
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.
add a comment |
add a comment |
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.
@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
add a comment |
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)
add a comment |
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.
@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
add a comment |
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.
@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
add a comment |
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.
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.
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
add a comment |
@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
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
edited Nov 10 at 5:01
answered Nov 10 at 4:56
JH Jang
263
263
add a comment |
add a comment |