r语言中生成一些随机数据,但是限定最大值不大于100,且都为整数

Python017

r语言中生成一些随机数据,但是限定最大值不大于100,且都为整数,第1张

能不能说得准确一点?是不是0到100都可以而且等可能?

可以用sample函数,比如:

samplesize<-100

sample(0:100,100,replace=TRUE)

用rand()函数

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std

int r(int fanwei)

{

srand((unsigned)time(NULL))//用于保证是随机数

return rand()%fanwei//用rand产生随机数并设定范围

}

int main()

{

cout<<r(100)<<endl//生成100以内的随机数,并显示

return 0

}

首先,如果想要别人复现出跟你一样的结果,要先设置随机种子

Set the seed of R‘s random number generator, which is useful for creating simulations or random objects that can be reproduced.

If you want to generate a decimal number where any value (including fractional values) between the stated minimum and maximum is equally likely, use the runif function.

The first argument is a vector of valid numbers to generate (here, the numbers 1 to 10), and the second argument indicates one number should be returned.

If we want to generate more than one random number, we have to add an additional argument to indicate that repeats are allowed: