可能重复:
生成随机枚举
我正在寻找从c ++中的枚举中选择元素的最佳方法.一开始,我感谢使用一个开关,但是,我有一些大的枚举,并没有效率,所以,我正在尝试使用每个人做一个.
要在枚举中选择水果元素:
enum FruitType
{
kApple,
kOrange,
kMelon
};
并尝试此功能:
/**
* R3turn a random fruit
*/
FruitType giveMekRandomFruit()
{
randNumber = rand % __TOTALFRUITS // Enum total = 3 elements
for (int& i: FruitType)
{
if (randNumber = i)
{
CCLog("Random Fruit selected:" + i);
return i; // return the number selected
}
}
}
显然不工作,我有一个syntaxis或概念的问题,任何想法???
谢谢你的时间.
我在R中进行分类.我有一个名为testD的测试数据数据框和一个称为结果的数据数据框(这些是正确的分类值).
这些数据帧非常大,在整个集合上训练我的模型需要太长时间,因此我想随机选择大约5,000个数据点并对其进行训练.
我知道如何从任一数据框中随机选择5,000行,但我需要确切地知道我从'testD'中选择了哪些行,因此我知道要比较的'结果'中有5,000行.
任何帮助将不胜感激!
我是 R 新手,在使用 Weibull 分布函数时遇到问题。
我必须使用累积分布,所以我必须使用 pweibull 函数。我也知道形状必须在 0.7 到 0.8 之间。
pweibull(q, shape, scale = 1, lower.tail = T, log.p = F)
Run Code Online (Sandbox Code Playgroud)
现在是棘手的部分,对于 q 参数,我需要传递通过 Weibull 逆计算的随机值,该逆将具有 0 到 1 之间的随机值作为输入。这个功能好用吗?
pdiweibull(x, q, beta)
Run Code Online (Sandbox Code Playgroud)
总而言之,如果我创建一个包含 100 个随机数的向量,其值从 0 到 1,将其作为“x”参数传递给 pdiweibull,然后将该结果作为“q”参数传递给 pweibull,我是否将我的想法正确地转换为 R 代码?
我被这个练习的另一部分卡住了。正在编码的程序允许您钻取短语(它为您提供一段代码,您写出英文翻译)并且我对“转换”功能的工作方式感到困惑。完整代码:http : //learnpythonthehardway.org/book/ex41.html
def convert(snippet, phrase):
class_names = [w.capitalize() for w in
random.sample(WORDS, snippet.count("%%%"))]
other_names = random.sample(WORDS, snippet.count("***"))
results = []
param_names = []
for i in range(0, snippet.count("@@@")):
param_count = random.randint(1,3)
param_names.append(', '.join(random.sample(WORDS, param_count)))
for sentence in snippet, phrase:
result = sentence[:]
# fake class names
for word in class_names:
result = result.replace("%%%", word, 1)
# fake other names
for word in other_names:
result = result.replace("***", word, 1)
# fake parameter lists
for word in param_names:
result = …Run Code Online (Sandbox Code Playgroud) Python 中是否有一个函数可以从 n 维 numpy 数组中采样并返回每次绘制的索引。如果不是,人们将如何定义这样一个函数?
例如:
>>> probabilities = np.array([[.1, .2, .1], [.05, .5, .05]])
>>> print function(probabilities, draws = 10)
([1,1],[0,2],[1,1],[1,0],[0,1],[0,1],[1,1],[0,0],[1,1],[0,1])
Run Code Online (Sandbox Code Playgroud)
我知道这个问题可以用一维数组以多种方式解决。但是,我将处理大型 n 维数组,不能仅仅为了进行一次绘制就重新调整它们的形状。
使用Array.prototype.reduce(或Array.prototype.reduceRight)签名,是否可以从数组中选择一个项目,所有索引的概率相等?这是我的尝试:
document.write(`
${[...'abcdefghijklmnopqrstuvwxyz'].reduce(function(last, next, index, array) {
if (Math.random() > index / array.length) {
return next;
}
return last;
})}
`);Run Code Online (Sandbox Code Playgroud)
在对此进行一些测试运行之后,分布似乎偏向较低的指数(也就是说更频繁地选择较高的指数).
假设我想要生成10个随机数的向量.我怎样才能在R和c ++中这样做,以便得到相同的向量?
我知道在C++中我必须使用srand()然后使用rand()而在RI中可以使用set.seed()然后使用runif.
但是我怎么能让他们变得一样呢?
到目前为止,我一直在使用一个列表来跟踪遇到的所有唯一数字.我使用随机数生成器来获得1到n之间的随机数.如果那个数字已经在我的列表中,那么我只是继续生成随机数,直到我遇到一个不在我的列表中的数字.当我得到一个不在我的列表中的新号码时,我将其添加到我的列表中并重复该过程,直到列表中的所有"n"号都存在.
显然,这种方法效率很低.有人可以提出一个有效的解决方案??
algorithm implementation probability permutation random-sample
以下R代码只给出正态分布的一半; 为了获得另一半,我应该改变什么代码?
halfnormal <- function(n){
vector <- rep(0,n)
for(i in 1:n){
uni_random <- runif(2)
y <- -log(uni_random)
while(y[2] < (y[1]-1)^2/2){
uni_random <- runif(2)
y <- -log(uni_random)
}
vector[i] <- y[1]
}
vector
}
output <- halfnormal(1000)
hist(output)
Run Code Online (Sandbox Code Playgroud) 是否可以使用SPARQL生成三元组的随机样本?我认为可能是通过SAMPLE函数,但这会返回一个SAMPLE.
我的解决方法是生成一个与OFFSET关键字一起使用的随机数,并使用LIMIT关键字返回所需的样本大小.我只是将偏移的随机数硬编码为200以方便,如下所示:
SELECT *
WHERE {
?s ?p ?o
}
OFFSET 200 #random number variable
LIMIT 100
Run Code Online (Sandbox Code Playgroud)
有没有更好的建议从SPARQL端点生成100个数据三元组的随机样本?
random-sample ×10
r ×4
arrays ×2
c++ ×2
python ×2
algorithm ×1
distribution ×1
enums ×1
for-loop ×1
function ×1
javascript ×1
numpy ×1
permutation ×1
probability ×1
reduce ×1
sparql ×1
statistics ×1
weibull ×1