本文介绍了随机浮动功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我尝试制作一个能够填充一半数组的函数,其中包含从-pi到+ pi的随机 双精度数。不幸的是,所有这一切 的回报都是-pi。我没有使用已经存在的随机数 生成函数,因为我不知道如何使用它们所以我 创建了我自己的基于来自INMOS的随机数发生器 Transputer Development系统,x [n + 1] =(1664525 x [n])mod 2 ^ 32 double modfloat(双a,双b) { 双c,d,x; c = a / b; d =(int32_t)c; x = cd; 返回x; } double dblrand(double x) { 返回modfloat((1664525 * x * pow(2,32)),pow(2,32) ))/ pow(2,32); } void phasenoise(double * s,int32_t M) { int32_t i; time_t seed = time(NULL); double pi = atan(1.0)* 4.0; s [M / 2 + 1] = dblrand((double)seed / pow(2,32))*(2 * pi) - pi; for(i = M / 2 + 2; i< M; i ++) s [i] = dblrand((s [i-1] + pi)/(2 * pi))*(2 * pi) - pi; } I tried making a function that would fill half of an array with randomdoubles that would go from -pi to +pi. Unfortunatly, all it everreturns is -pi. I haven''t used an already existing random numbergenerating function because I wasn''t sure to know how to use them so Icreated my own based on the random number generator from the INMOSTransputer Development system which is x[n+1] = (1664525 x[n]) mod 2^32 double modfloat(double a, double b){double c, d, x; c=a/b;d=(int32_t) c;x=c-d;return x;} double dblrand(double x){return modfloat((1664525 * x * pow(2, 32)), pow(2, 32)) / pow(2, 32);} void phasenoise(double *s, int32_t M){int32_t i;time_t seed=time(NULL);double pi=atan(1.0)*4.0; s[M/2+1]=dblrand((double) seed/pow(2, 32)) * (2*pi) - pi;for (i=M/2+2; i<M; i++)s[i]=dblrand((s[i-1]+pi) / (2*pi)) * (2*pi) - pi;} 推荐答案 注意: 如果随机数字的数量没有发挥那么大的作用, 然后使用(双)rand()/ RANDMAX代替(使用适当的种子 由srand()设置)。 否则,使用另一个现成的发电机 - 它可能比你的发电机具有更好的数学特性。不过这不是讨论这个问题的好地方。 我宁愿使用符号常量代替 pow (2,32)和pi。 前者的便捷方式是 #define POW2TO32_DBL((1UL<< 31)* 2.0) 对于后者,只需要足够数字的pi。 干杯 Michael - 电子邮件:我的是/ at / gmx / dot / de地址。 Notes:If the number of random digits does not play that much of a role,then use (double)rand()/RANDMAX instead (with an appropriate seedset by srand()).Otherwise, use another off-the-shelf generator -- it probably hasbetter mathematical properties than your generator. This is notthe right place to discuss this, though. I would rather work with symbolic constants instead ofpow(2, 32) and pi.A convenient portable way for the former is#define POW2TO32_DBL ((1UL << 31)*2.0)For the latter, just take enough digits of pi.CheersMichael--E-Mail: Mine is an /at/ gmx /dot/ de address. BTW:你需要发明modfloat(),改用fmod()。 BTW: You do need to invent modfloat(), use fmod() instead. 哦谢谢我以前从未听说过它。 oh thanks i had never heard about it before. ^^^^^^^^^^ 失去那一个。 a * b %b == 0 所以,你总是得到0.0附近的东西,这导致你的缩放到-pi。 ^^^^^^^^^^ Lose that one. a*b%b == 0 So, you always get back something near 0.0, which leads with your scaling to -pi. 是啊但是不,x是介于0和1之间的双倍,如果我这样做,那就是在它被分割之前得到 回到之前的结果。 yeah but no, x is a double between 0 and 1, if i do that it''s to getback to the previous result before it''s divided. 注意:如果随机数字的数量没有发挥那么大的作用,那么使用(双)rand()/ RANDMAX代替(使用适当的种子由srand()设置。 Notes: If the number of random digits does not play that much of a role, then use (double)rand()/RANDMAX instead (with an appropriate seed set by srand()). ok,所以我该怎么做(这就是我听起来像新手的地方),我做的> 类似于srand(time(NULL));然后(双)rand()/ RANDMAX ?? 而且我承认,使用随机生成器只有2 ^ 15 的可能性让我感到很恼火(对吧?)但我想它应该没问题。 否则,使用另一个现成的发电机 - 它可能比你的发电机具有更好的数学特性。不过,这不是讨论这个问题的正确场所。 我宁愿使用符号常量而不是 pow(2,32)和pi。对于前者来说便捷的方便是 #define POW2TO32_DBL((1UL<< 31)* 2.0) 我不知道是什么((1UL<< ; 31)* 2.0)可以意味着.. 对于后者,只需要取足够数字的pi。 ok, so how do I do that (that''s where I sound like a newbie), i dosomething like srand(time(NULL)); and then (double) rand()/RANDMAX??And I admit it kinda annoys me to use a random generator with only 2^15possibilities (right?) but I guess it should be alright. Otherwise, use another off-the-shelf generator -- it probably has better mathematical properties than your generator. This is not the right place to discuss this, though. I would rather work with symbolic constants instead of pow(2, 32) and pi. A convenient portable way for the former is #define POW2TO32_DBL ((1UL << 31)*2.0)I have no idea what ((1UL << 31)*2.0) can mean.. For the latter, just take enough digits of pi. 什么是错的用我做pi的方式? What''s wrong with the way I do my pi? uint32_t,如果有的话。但是,这也不能保证工作。 uint32_t, if anything. However, this is not guaranteed to work either. BTW:你确实需要发明modfloat(),而是使用fmod()。 BTW: You do need to invent modfloat(), use fmod() instead. ^^^^^^^^^ ^ 失去那一个。 a * b%b == 0 所以,你总是得到0.0附近的东西,这导致你的缩放到-pi。 ^^^^^^^^^^ Lose that one. a*b%b == 0 So, you always get back something near 0.0, which leads with your scaling to -pi. 注意:如果随机数字的数量不能播放那个mu ch的角色,然后使用(双)rand()/ RANDMAX(用srand()设置适当的种子)。否则,使用另一个现成的生成器 - 它可能比你的发电机具有更好的数学特性。不过,这不是讨论这个问题的正确场所。 我宁愿使用符号常量而不是 pow(2,32)和pi。对于前者来说便捷的便携方式是 #define POW2TO32_DBL((1UL<< 31)* 2.0)对于后者,只需要足够数字的pi。 干杯迈克尔 - 电子邮件:我的是/ at / gmx / dot / de地址。 Notes: If the number of random digits does not play that much of a role, then use (double)rand()/RANDMAX instead (with an appropriate seed set by srand()). Otherwise, use another off-the-shelf generator -- it probably has better mathematical properties than your generator. This is not the right place to discuss this, though. I would rather work with symbolic constants instead of pow(2, 32) and pi. A convenient portable way for the former is #define POW2TO32_DBL ((1UL << 31)*2.0) For the latter, just take enough digits of pi. Cheers Michael -- E-Mail: Mine is an /at/ gmx /dot/ de address. 我使用fmod()而不是我的modfloat()现在它的工作原理。好吧,现在 值似乎非常随机并且在正确的范围内,所以我想我会继续使用我的dblrand()函数。非常感谢您的帮助! I used fmod() instead of my modfloat() and now it works. Well, now thevalues seem to be quite random and in the right range, so I guess I''llkeep using my dblrand() function. Thanks alot for help! 这篇关于随机浮动功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 10:26