visual c ++中的randome数字 [英] randome number in visual c++

查看:124
本文介绍了visual c ++中的randome数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想生成随机添加在视觉C + +

但是rand和srand函数不能产生真正好的数字并且分配没有产生数字是相同的数字在不同的表演中。

我正在制作一个构成opengl一部分的视频项目,应该逐渐在空间中随机分发。但是,我不能这么随机的数字。

现在你已经找到了一种生成随机数的方法,对吗?

有没有办法在C#中生成一个随机数我用c?像头文件一样?

我很感谢你的指导

Hi I want to generate random Add in visual C + +
But the rand and srand function does not produce really good numbers and distribution have not produced the numbers are the same numbers in different performances.
I'm working on a video project that forms part of opengl should be distributed randomly in space gradual've written., But I just can not so random numbers.
Now you have found a way to generate a random number, right?
Is there a way to generate a random number in C # I use the c? Like a header file?
I am grateful for your guidance

推荐答案

就像C一样,随机数序列依赖于起始值 - 种子 - 所以如果你创建两个具有相同种子的序列,它们将是相同的。通常,种子是从系统时钟加载的,所以如果你同时创建两个单独的随机实例,你将获得相同的序列,即使你稍后再使用它们一小时。



看看你的代码:你使用rand和srand的可能性很大,这会导致你的问题。 [ ^ ]可能会有所帮助。

如果您使用的是C#,正如您在问题末尾所建议的那样(或CLI C ++)然后你可以使用随机类 [ ^ ]但在这种情况下你应该使用并初始化Random instance作为类成员,不在方法/函数中创建它,因为它会产生与上面提到的srand相同的问题。
Just like C, random number sequences are dependant on the the starting value - the seed - so if you create two sequences with the same seed they will be identical. Mostly, seeds are loaded from the system clock, so if you create two separate random instances at the same time, you will get the same sequence, even if you later use them an hour apart.

Look at your code: the chances are that you are using rand and srand badly and this is causing your problem. This[^] may help.
If you are using C#, as you suggest at the end of your question (or CLI C++) then you can use the Random class[^] from .NET, but in this case you should use and initialise the Random instance as a class member, not create it in a method / function as that will create the same problem as the srand mentioned above.


这篇关于visual c ++中的randome数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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