从另一个数组创建一个随机数组
我试图找出一种方法来“洗牌”数组,而无需使用随机或numpy的内置混音函数。
我有以下代码,但是它将多次选择一个单元格,并且我只想在Deck1中使用这些值。但是,如果Deck1有两个实例为3(例如),那么它应该导致两个实例在Deck2中的3个实例,
我怀疑这应该是为deck2搜索,因为它是为deck1中的值而构建的,但是我不确定如何去关于它,尤其是如果Deck1具有多个价值的实例。
def shuffler():
n = int(input(" Please enter the number of cards in the deck: "))
s = input("Should the deck be shuffled? (Y/N): ").lower()
# Create the decks
# First deck is filled with either random or ordered numbers
if s == "y":
Deck1 = np.random.randint(low = 0, high = n, size = n)
print("Original deck:")
print(Deck1)
Deck2 = np.empty(n, np.int)
else:
Deck1 = np.arange(n)
print("Original deck:")
print(Deck1)
Deck2 = np.empty(n, np.int)
for i in Deck1:
j = np.random.randint(0, n)
Deck2[i] = Deck1[j]
print("Shuffled deck:")
print(Deck2)
I am trying to figure out a way to "shuffle" an array without using inbuilt shuffle functions of random or numpy.
I have the following code, but it will select a cell more than once, and I would like to use only the values in Deck1. However, if Deck1 has two instances of 3 (for instance) then it should result in two instances of 3 in Deck2
I suspect this should be searching Deck2 as it is built for the value in Deck1, but I am not completely sure how to go about it, especially if Deck1 has multiple instances of a value.
def shuffler():
n = int(input(" Please enter the number of cards in the deck: "))
s = input("Should the deck be shuffled? (Y/N): ").lower()
# Create the decks
# First deck is filled with either random or ordered numbers
if s == "y":
Deck1 = np.random.randint(low = 0, high = n, size = n)
print("Original deck:")
print(Deck1)
Deck2 = np.empty(n, np.int)
else:
Deck1 = np.arange(n)
print("Original deck:")
print(Deck1)
Deck2 = np.empty(n, np.int)
for i in Deck1:
j = np.random.randint(0, n)
Deck2[i] = Deck1[j]
print("Shuffled deck:")
print(Deck2)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些评论:
deck1 = np.random.randint(low = 0,high = n,size = n)
不是初始化甲板的好方法,因为它应该包含n
唯一卡。您应该使用deck1 = np.arange(n)
。改组甲板的一种方法是创建一个随机的位置阵列,然后在这些位置中交换项目。例如:
产量
数组([6,3,7,4,6,9,2,6,7,4])
。考虑这是将卡在位置0
移动到位置6
的方法。位置的卡6
可以向右移动,也可以移至位置0
。同样,位置上的卡1
转到3
,3
的卡转到1
。这就是我实施您的代码的方式:
屈服
A few comments:
Deck1 = np.random.randint(low = 0, high = n, size = n)
isn't a very good way of initializing a deck since it's supposed to containn
unique cards. You should useDeck1 = np.arange(n)
.A way of shuffling a deck is to create a random array of positions, then swap the items in these positions. For example:
yields
array([6, 3, 7, 4, 6, 9, 2, 6, 7, 4])
. Consider this a way of moving the card at position0
to position6
. The card at position6
could either be shifted to the right, or move to position0
. Similarly, the card at position1
goes to3
, and the card at3
goes to1
.This is how I implemented your code:
Yielded
您可以编写自己的洗牌功能,但要小心进行均匀的分布。这是 durstenfeld shuffle shuffle algorithm:
然后在您的代码中:
You can write your own shuffle function, but be careful to have a homogeneous distribution. Here is an implementation of the Durstenfeld shuffle algorithm:
Then in your code: