C程序 - srand()函数 [英] C program - srand()

查看:165
本文介绍了C程序 - srand()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  推荐方式初始化函数srand?

我在c。使用srand()函数时有以下问题。

I have the following problem when using srand() in c.

我打电话函数srand(时间(NULL))在一个循环,但环路的结束前1 秒,每次我叫兰特()我得到同样的值。

I call srand(time(NULL)) in a loop, but the loop finishes before 1 sec and every time I call the rand() I get the same value.

我该如何解决呢?

推荐答案

由于种子被绑定到这是自从秒Unix纪元的时间(),基本上你给它,因为循环只需不到一个相同的种子第二。

because the seed is bound into the time() which are seconds since unix epoch, basically you're giving it the same seed because the loop takes less than a second.

你应该做的就是在几微秒的时间。看看函数gettimeofday(),如果你的编码Windows适用的Google微秒的win32 C,你介意需要从双转换为integerso只是这样做(unsigned int类型)双* 100.0f;

What you should do is get the time in microseconds. Take a look at gettimeofday() if you're coding for windows google microseconds win32 C, you mind need to convert it from double to integerso just do this (unsigned int)double * 100.0f;

这篇关于C程序 - srand()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆