我试图运行一个包含srand函数的代码:
srand(1);
my @x = (1..1000).roll(100);
my @y = (200..7000).roll(100);
say sum(@x);
say sum(@y);
say $*KERNEL
从文档中可以很清楚地看出,srand是。当我在Windows 10上测试这个时,我得到
46221
375477
win32
当我在glot.io 中测试它时,我得到
50941
405340
linux
在tio.run ,我得到
47784
354115
linux (5.2.11.100.fc.29.x.86._.64)
在repl
我需要用C编写UNIX/LINUX的srand48和drand48函数。我坚持设置和使用种子值。我有两个函数:
#include <math.h>
long int mfml_win_drandX0; //actual value used for generation
void srand48(long int seedval)
{
mfml_win_drandX0=seedval; //setting seed into mfml_win_drandX0
}
double drand48(void)
{
static const double a=0
是否允许srand(0)具有与srand(1)相同的效果?
C11,7.22.2.2 srand函数(结缔组织增加):
srand函数使用参数作为新序列的种子,伪随机数将通过随后对rand的调用返回。
然而,在glibc中,srand(0)有作为srand(1)。
/* We must make sure the seed is not 0. Take arbitrarily 1 in this case. */
if (seed == 0)
seed = 1;
因此,伪随机数的通过对rand的后续调用返回,这是令人困惑的.
额外:我们看到,在MSVC中,srand
我需要生成密码学学生家庭作业的随机数。
一开始我想用
srand(time(NULL));
int r = rand();
但我读到不应该把它用于密码学。如果可能的话,我希望而不是编写我自己的伪随机数生成器。
为了密码学的目的,在C中生成伪随机数的最快/最简单的方法是什么?
我在Linux上,我不介意答案是不是跨平台的。
我试着用srand和时间作为随机值。如果我看srand函数,它需要无符号int,但是如果我们用时间初始化它,它似乎有很长的值,所以编译器总是抱怨没有精确警告。有什么更好的方法吗?
// srand definition
void srand (unsigned int seed);
// using srand with time to seed random value
srand(time(NULL));
警告712:(信息-精度损失(arg )。(长到未签署的int))
为了测试,我需要可重复的随机数。随机性没那么重要。目前,我正在设置一个种子std::srand(x),并使用std::rand()生成。正如预期的那样,在具有相同种子的多次运行中,数字总是相同的。
对于其他部分,我需要UUID。我使用了utils-linux libuuuid。
但是运行以下代码:
std::srand(2);
uuid_t uuid;
int x = std::rand();
uuid_generate(uuid);
int y = std::rand();
y在每次运行中都是不同的。
我已经尝试过使用uuid_generate_time,uuid
我想知道如何在一个固定位置得到一个随机数,所以当我回到那个位置时,我会看到相同的随机数。
首先,我尝试了这样的方法:
int getRand( int x , int y )
{
srand( x * y );
return rand();
}
但是这里-1 * -1和1 *1是一样的,所以我会得到很多对称性。
然后我试着:
int getRand( int x , int y )
{
srand( x * ( 1 << 12 ) + y );
return rand();
}
和
int getRand( int x , int y ) //
我需要将这个C代码移植到Rust中:
QueryPerformanceFrequency((unsigned long long int *) &frequency);
我没有找到这样的函数。
Linux版本如下所示:
struct timespec now;
if (clock_gettime(CLOCK_MONOTONIC, &now) == 0)
frequency = 1000000000;
我应该打电话给std::time::Instant::now()并将频率设置为1000000000吗?
这是一个完整的功能:
// Initializes hi-resol
我已经用头撞了半个小时了,不知道怎么回事。我试图生成10个随机数的列表,1-100。但是当我运行它时,它们都是相同的号码。太令人沮丧了!我认为这是因为这个数字仍然存储在内存中,但是在对随机数和变量进行三次再随机化之后,它仍然得到了相同的数字。我做错了什么?
代码:
main() {
int i;
int randnum;
srand(time(NULL));
randnum = rand() % 2;
for (i = 0; i < 10; i++) {
srand(time(NULL));
randnum = ra
我只想知道这个srand函数是如何工作的通过传递时间(NULL)作为参数
int Secret; // variable declaration
srand(time(NULL)); //calling srand function
Secret = rand() % 10 + 1; //generation random no between 0 and 10
cout<<Secret;
我使用这个函数生成随机整数。
int rnd(int min, int max)
{
static int srand(time(0));
return min + rand() % max;
}
是对的吗?可能更好的移动srand(time(0));的主要功能?如下所示:
int rnd(int min, int max){
return min + rand() % max;
}
int main(){
srand(time(0));
..............
}
当你这样做时,C rand()和srand()函数是非常有用的:
srand(SEED);
for()
{
//doing something with one thing using rand()
}
srand(SEED);
for()
{
//doing something with other thing using rand()
}
我可以在SystemVerilog中有这样的东西吗?是的,我知道$urandom(SEED),但问题是它应该扫描一次,然后使用rand()多次
据我所知,如果不更改种子号,rand()函数将生成相同的运行号。这就是srand()进来的原因。时间总是在变化,所以我知道您应该将time(null)参数传递给srand。我的问题是下面的代码从一个教程网站。
int main()
{
int i, n=5;
time_t t;
/* Intializes random number generator */
srand((unsigned) time(&t));
/* Print 5 random numbers from 0 to 50 */
for( i = 0 ; i <