gpt4 book ai didi

r - 生成随 secret 码的函数

转载 作者:行者123 更新时间:2023-12-03 03:16:34 24 4
gpt4 key购买 nike

我想使用以下函数为员工生成一个随 secret 码。这是我第一次尝试 R 中的函数。所以我需要一些帮助。

genPsw <- function(num, len=8) {
# Vorgaben für die Passwortkonventionen festlegen
myArr <- c("", 2, 3, 4, 5, 6, 7, 8, 9, "A", "B",
"C", "D", "E", "F", "G", "H", "J", "K", "L", "M",
"N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"!", "§", "$", "%", "&", "(", ")", "*")
# replicate is a wrapper for the common use of sapply for repeated evaluation of an expression
# (which will usually involve random number generation).
replicate(num, paste(sample(myArr, size=len, replace=T), collapse=""))
# nrow of dataframe mitarbeiter
dim_mitarbeiter <- nrow(mitarbeiter)
for(i in 1:dim_mitarbeiter) {
# Random Number Generation with i
set.seed(i)
# Generate Passwort for new variable password
mitarbeiter$passwort <- genPsw(i)
}

}

根据 Floo0 的答案,我已将函数更改为类似的内容,但它不起作用:

genPsw <- function(num, len=8) {
# Vorgaben für die Passwortkonventionen festlegen
sam<-list()
sam[[1]]<-1:9
sam[[2]]<-letters
sam[[3]]<-LETTERS
sam[[4]]<-c("!", "§", "$", "%", "&", "(", ")", "*")

# nrow of dataframe mitarbeiter
dim_mitarbeiter <- nrow(mitarbeiter)
for(i in 1:dim_mitarbeiter) {
# Random Number Generation with i
tmp<-mapply(sample,sam,c(2,2,2,2))
# Generate Passwort for new variable password
mitarbeiter$passwort <- paste(sample(tmp),collapse="")
}

}

最佳答案

怎么样

samp<-c(2:9,letters,LETTERS,"!", "§", "$", "%", "&", "(", ")", "*")
paste(sample(samp,8),collapse="")

结果是这样的

"HKF§VvnD"

注意:此方法不强制使用大写、数字和非字母数字符号

编辑:

如果您想强制使用一定数量的大写字母、数字和非字母数字符号,您可以这样做:

sam<-list()
sam[[1]]<-1:9
sam[[2]]<-letters
sam[[3]]<-LETTERS
sam[[4]]<-c("!", "§", "$", "%", "&", "(", ")", "*")

tmp<-mapply(sample,sam,c(2,2,2,2))
paste(sample(tmp),collapse="")

其中c(2,2,2,2)指定数字、字母、大写字母和symbild的数量(按此顺序)。结果:

[1] "j$bP%5R3"

编辑2:要在表中生成新列 mitarbeiter 只需使用

passwort<-replicate(nrow(mitarbeiter),paste(mapply(sample,sam,c(2,2,2,2)),collapse=""))
mitarbeiter$passwort<-passwort

关于r - 生成随 secret 码的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22219035/

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