标签: random

如何在R中的随机位置向向量中引入值?

我刚刚开始学习R,我的任务是创建一个具有正态分布的10000个的向量,mean = 0并且sd = 100.我做了什么.

x <- rnorm(10000, mean = 0, sd = 100)
Run Code Online (Sandbox Code Playgroud)

但现在我被要求values between 500 and 700在该向量中引入1000个随机位置.

谁能帮我?

random r vector

-4
推荐指数
1
解决办法
905
查看次数

rand()随机数生成C++

我正在尝试使用该rand()函数生成伪随机整数.它的工作原理,但我的问题是它始终为int选择相同的名称.(在这种情况下,它是41.我想如果你把rand()主要的while循环放在85或者什么东西.)

有没有办法来解决这个问题?这是我的代码:

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>

    int guess;
    int danse = rand() % 101;

    using namespace std;

    void more(){
    cout << "The number that you need to guess is higher!";

    return;
    }

    void lower(){
    cout << "The number that you need to guess is lower!";

    return;
    }

    int main(){
    while(1){
    cout << "\nGuess a number 0-100: ";
    cin >> guess;

    if (guess > danse){
        lower();
}
    if (guess < danse){
        more();
}

    if (guess …
Run Code Online (Sandbox Code Playgroud)

c++ random

-4
推荐指数
1
解决办法
1311
查看次数

将具有随机唯一数字的数组转换为具有顺序进行数字的数组?

假设我有一个包含唯一随机数的数组(其中数字的可能范围很小,为0到20).例如:

[6, 3, 11, 9, 4, 5]
Run Code Online (Sandbox Code Playgroud)

如何将以下数组转换为以下内容:

[3, 0, 5, 4, 1, 2]
Run Code Online (Sandbox Code Playgroud)

第二个数组从0开始,以(array.Length-1)结束,但放置与第一个数组中的数量相关.

如何在C/C++/C#中以有效的方式实现这一点?(对方法更感兴趣)

我举了一个例子.它可以是任何东西:

[7, 10, 0, 19, 50, 33, 45, 100]
[1, 2,  0,  3,  6,  4,  5,   7]
Run Code Online (Sandbox Code Playgroud)

数组A中最小的数字在数组B中为0.数组A中的最大数字是数组B中的(array.Length-1).数组A可以是完全随机的(只是它永远不会包含两个或多个相同的数字),但数组A必须以与数组A中相同的顺序包含从0到array.Length-1)的所有数字.

c c# arrays random numbers

-4
推荐指数
1
解决办法
210
查看次数

请在C中解释这种歧义

当我编译这个程序时,我得到一些随机数作为输出.在Cygwin中输出为47但在RHEL5中,它给出一些负的随机数作为输出.谁能告诉我原因?
码:

main()
{
printf("%d");
}
Run Code Online (Sandbox Code Playgroud)

c random

-5
推荐指数
1
解决办法
158
查看次数

C++随机函数返回相同的数字

我试图在0到3的范围内得到随机数.使用这样的代码:

#include <cstdlib>
int index = rand() % 3;
Run Code Online (Sandbox Code Playgroud)

但总是得到相同的结果:1 1 0 1.

我做错了什么?总是相同的数字.结果应在每次编译后或运行时更改其值?

c++ random

-5
推荐指数
1
解决办法
270
查看次数

如何从之前选择的Javascript中的单词和短语列表中生成随机选择的单词或短语?

对于我在高中的计算机编程课程,我需要在学期结束时制作一个程序.目前我正在考虑做一个Black Ops 2加载生成器,为此我需要在Javascript中生成随机单词.我希望能够从单词列表中生成一个单词.所以如果我的单词列表是这样的:

Horse,
Pig,
Dog,
Cat,
Parrot,
Iguana,
Run Code Online (Sandbox Code Playgroud)

然后我希望能够随机生成其中一个,然后在文本框中显示它.

html javascript random words

-5
推荐指数
1
解决办法
8156
查看次数

修复一个函数随时间返回重复?

我在这里有一个返回4位数字符串的函数.问题是,当我运行500次或更多次的函数时,它开始返回重复项.怎么避免呢?

我的职责:

import random
def CreatePass():
    Num = str(random.randint(1000, 9999)
    return Num
Run Code Online (Sandbox Code Playgroud)

python random

-5
推荐指数
1
解决办法
107
查看次数

比较值:左值作为赋值的左操作数所需的左值

为什么我不能比较随机函数的结果?

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main (){

srand(time(NULL));

/*Player 1*/
int P1d1, P1d2, P1total;
P1d1 = 1 + rand() % 6;  //Rolls Player 1's first die; random # between 1-6
P1d2 = 1 + rand() % 6;  //Rolls Player 1's second die; random # between 1-6
P1total = P1d1 + P1d2;  //Takes total of both rolls

/*Player 2*/
int P2d1, P2d2, P2total;
P2d1 = 1 + rand() % 6; //Rolls Player 2's first die; random # between 1-6
P2d2 = …
Run Code Online (Sandbox Code Playgroud)

c random lvalue

-5
推荐指数
1
解决办法
2万
查看次数

在c ++中生成超级随机数

C++ 11引入了允许生成非常随机数的类,它还创建了随机数的均匀分布.还有实现生成种子(用于使随机数生成器更随机的数字).

我试图创建一个在最小和最大之间生成随机数的函数,但我遇到了麻烦.该函数仅生成种子和随机数一次.换句话说,当我调用该函数时,它将继续给我相同的数字.

下面是代码,我尝试生成一堆种子,随机选择其中一个,将该种子用于RNG并最终产生一个随机数.

int Utils::GenerateSuperRandomNum(int min, int max)
{
    //Seed a the RNG
    int randNum;
    int randIndex;
    seed_seq seq{ 1, 2, 3, 4, 5 };
    vector<int> seeds(5 * max);
    uniform_int_distribution<int> rngDistribution(min, max);    //Generates number in the range min to max.

    //Generate our seed numbers.
    seq.generate(seeds.begin(), seeds.end());

    //Generate random index bewteen 0 and size - 1.
    srand(seeds.at(0));
    randIndex = rand() % seeds.size(); 

    //Seed the RNG with a random seed from our vector.
    mt19937 rngGenerator(seeds.at(randIndex)); 

    //Get a random number.
    randNum = …
Run Code Online (Sandbox Code Playgroud)

c++ random vector c++11 random-seed

-5
推荐指数
1
解决办法
987
查看次数

了解 .NET 中的哈希代码

到目前为止我收集到的信息是,哈希码是整数,有助于更快地从数组中查找数据。看这段代码:

string x = "Run the program to find this string's hash code!";
int hashCode = x.GetHashCode();
Random random = new Random(hashCode);

for(int i = 0; i<100; i++)
{
// Always generates the same set of random integers 60, 23, 67, 80, 89, 44, 44 and so on...
int randomNumber = random.Next(0, 100);

Console.WriteLine("Hash Code is: {0}", hashCode);
Console.WriteLine("The random number it generates is: {0}", randomNumber);

Console.ReadKey();
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我使用字符串的哈希码x作为随机数生成器的种子。这段代码给了我 100 个随机整数,但每次运行该程序时,它都会给我相同的随机数集!我的问题是:为什么每次循环迭代时它都会给我一个不同的随机数?为什么 x 的哈希码不断变化,即使字符串没有改变。哈希码到底是什么以及它们是如何生成的(如果需要)?

.net c# random hash hashcode

-5
推荐指数
1
解决办法
519
查看次数

标签 统计

random ×10

c ×3

c++ ×3

c# ×2

vector ×2

.net ×1

arrays ×1

c++11 ×1

hash ×1

hashcode ×1

html ×1

javascript ×1

lvalue ×1

numbers ×1

python ×1

r ×1

random-seed ×1

words ×1