gpt4 book ai didi

在R中反复洗牌

转载 作者:行者123 更新时间:2023-12-02 17:01:37 25 4
gpt4 key购买 nike

我写的代码迷宫有问题,会反复洗牌。我写了一句让我的腰洗牌的台词,但每次牌组都以同样的方式洗牌。我需要帮助编写代码,以便每次都以不同的方式洗牌。

shuffling <-sample(deck,length(deck))

最佳答案

这是一个基本的 R 解决方案,它从一组 10 张卡片中生成一个种子向量和 10 个样本。

seeds <- runif(10)*1000000
cards <- 1:10

shuffledDecks <- lapply(seeds,function(aSeed){
set.seed(aSeed)
sample(cards)
})

当我们在 RStudio 对象查看器中检查列表时,我们看到 10 次洗牌中的所有牌顺序都是不同的。

enter image description here

如果我们应用其中一种方法从 Creating a Deck of Cards Without Using While and Double For Loop 创建卡片组,一副 52 张扑克牌的一组 10 次洗牌看起来像这样。

seeds <- runif(10)*1000000
cards = c(2:10, "J", "Q", "K", "A")
suits = c("♠", "♥", "♦", "♣")
deck <- paste0(rep(cards, length(suits)), #card values
rep(suits, each = length(cards))) #suits

shuffledDecks <- lapply(seeds,function(aSeed){
set.seed(aSeed)
sample(deck)
})

shuffledDecks[[1]]
shuffledDecks[[2]]

...和输出:

> shuffledDecks[[1]]
[1] "3♥" "Q♠" "6♦" "8♠" "9♠" "2♦" "10♥" "J♥" "7♦" "6♣" "7♠" "3♣" "2♠" "8♦" "8♥" "A♦" "10♣" "A♥" "5♦" "J♦"
[21] "A♠" "4♥" "7♣" "4♣" "3♠" "6♠" "5♥" "K♠" "5♣" "5♠" "J♣" "6♥" "9♥" "K♣" "Q♥" "9♣" "A♣" "9♦" "3♦" "K♦"
[41] "4♦" "J♠" "4♠" "8♣" "10♠" "Q♦" "Q♣" "K♥" "7♥" "2♣" "2♥" "10♦"
> shuffledDecks[[2]]
[1] "7♠" "5♦" "6♥" "Q♣" "K♥" "J♦" "Q♠" "Q♥" "3♦" "4♥" "2♦" "A♠" "9♦" "4♣" "7♣" "8♦" "4♠" "5♠" "K♦" "3♥"
[21] "6♣" "A♣" "10♥" "8♠" "8♣" "J♣" "J♠" "10♦" "7♦" "5♣" "10♣" "9♣" "K♣" "K♠" "8♥" "6♠" "2♠" "5♥" "3♣" "J♥"
[41] "A♦" "3♠" "4♦" "9♥" "A♥" "6♦" "9♠" "7♥" "2♥" "10♠" "2♣" "Q♦"
>

关于在R中反复洗牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53944573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com