如何在CircuitPython中对1到X的整数列表进行无重复随机化?

huangapple go评论69阅读模式
英文:

How can I randomize a list of integers 1 to X in CircuitPython without duplicates?

问题

我正在创建一个 CircuitPython 项目,代码编译正常,但它不按照我的意图运行。我有一个循环,它应该生成一个不重复的整数列表,从1到X,随机排序。它确实这样做,除了1和2总是以某种奇怪的原因复制在其中。因为这是在 CircuitPython 中,我不能使用 random.sample,所以这是一个简单的代码,但它仍然不起作用。

编辑:我需要列表最终变成字符串列表而不是整数列表。我没有清楚表达这一点。

station = 20

songs = []
loop = True
while loop == True:
    r=random.randint(1, station)
    if r not in songs:
        songs.append(str(r))
    if len(songs) == station:
        loop = False

输出: ['14', '3', '11', '20', '10', '17', '9', '5', '19', '1', '6', '18', '15', '8', '2', '4', '7', '12', '16', '13']

在这种情况下,2不会重复出现,但1会。我只想要一个精确的1到X的列表,随机排序,没有重复项。

英文:

I am creating a circuit python project and the code compiles fine but it isn't working the way I intend. This loop that I have is supposed to generate a non repeating list of integers 1 to X in a random order. It does that except 1 and 2 are always copied in there for some odd reason. Because this is in circuit python, I cannot use random.sample so thats why this is simple code but it still doesn't work.

Edit: I do need the list to end up as a list of strings instead of a list of Integers. I failed to make that clear.

station = 20

songs = []
loop = True
while loop == True:
    r=random.randint(1, station)
    if r not in songs:
        songs.append(str(r))
    if len(songs) == station:
        loop = False

Output: ['14', '3', '11', '20', '10', '17', '9', '5', '19', '17', '1', '6', '18', '1', '9', '15', '6', '11', '14', '8', '2']

In this case 2 isn't in there twice but 1 is. I just want an exact list of 1 - X randomized with no duplicates.

答案1

得分: 1

将字符串放入列表中,但将其与整数进行比较。立即将其转换为字符串,以便使用一致的类型。

while len(songs) < station:
    r = str(random.randint(1, station))
    if r not in songs:
        songs.append(r)
英文:

You're putting strings in the list but comparing them with ints. Convert it to a string immediately so you use consistent types.

while len(songs) &lt; station:
    r = str(random.randint(1, station))
    if r not in songs:
        songs.append(r)

答案2

得分: 0

随机洗牌不难编写,是解决这个问题的较好方法之一:

import random

station = 20
def shuffle(lst):
    for i in range(len(lst)):
        j = random.randrange(0, len(lst))
        lst[i], lst[j] = lst[j], lst[i]

songs = list(range(1, station+1))
print(songs)
shuffle(songs)
print(songs)

后续

好的,让我们进行两个小的修改,以改善随机性。 在这种特殊情况下,差异微不足道,但在研究情境中,这可能很重要:

import random

station = 20
def shuffle(lst):
    for i in range(len(lst)-1):
        j = random.randrange(i, len(lst))
        lst[i], lst[j] = lst[j], lst[i]

songs = list(range(1, station+1))
print(songs)
shuffle(songs)
print(songs)
英文:

Random shuffle is not hard to write, and is one of the better approaches to this problem:

import random

station = 20
def shuffle(lst):
    for i in range(len(lst)):
        j = random.randrange(0,len(lst))
        lst[i],lst[j] = lst[j],lst[i]

songs = list(range(1,station+1))
print(songs)
shuffle(songs)
print(songs)

Followup

OK, let's make two small modifications that improve the randomness. In this particular case, the difference is insignificant, but in a research situation, it could be important.

import random

station = 20
def shuffle(lst):
    for i in range(len(lst)-1):
        j = random.randrange(i,len(lst))
        lst[i],lst[j] = lst[j],lst[i]

songs = list(range(1,station+1))
print(songs)
shuffle(songs)
print(songs)

huangapple
  • 本文由 发表于 2023年6月2日 06:08:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定