-1

Using C, how can I generate a random point on the complex plain?

4

1 回答 1

1

作为一个不错的起点,您可以使用rand(). 要生成介于 -2 和 +2 之间的随机数,您可以执行以下操作:

float f = 4 * ((rand() / (float)RAND_MAX) - 0.5f);

如果你重复这个过程,你可以得到 anx和 a y

float x = 4 * ((rand() / (float)RAND_MAX) - 0.5f);
float y = 4 * ((rand() / (float)RAND_MAX) - 0.5f);

现在只需使用xy作为您的复数。

此外,请务必srand()在程序开始时调用一次(且仅一次)以播种随机数生成器。

于 2012-11-17T04:17:55.277 回答