随机浮点数

发布于 2024-08-10 20:56:40 字数 1290 浏览 4 评论 0原文

我编写这个函数是为了获得 0 .. 1(含)之间的伪随机浮点数:

float randomFloat()
{
      float r = (float)rand()/(float)RAND_MAX;
      return r;
}

但是,它始终返回 0.563585。无论我运行控制台应用程序多少次,数字都是相同的。

编辑:

如果需要的话,这是我的整个申请:

#include <stdio.h>
#include <stdlib.h>

float randomFloat()
{
      float r = (float)rand() / (float)RAND_MAX;
      return r;
}

int main(int argc, char *argv[])
{
  float x[] = {
        0.72, 0.91, 0.46, 0.03, 0.12, 0.96, 0.79, 0.46, 0.66, 0.72, 0.35, -0.16,
        -0.04, -0.11, 0.31, 0.00, -0.43, 0.57, -0.47, -0.72, -0.57, -0.25,
        0.47, -0.12, -0.58, -0.48, -0.79, -0.42, -0.76, -0.77
  };

  float y[] = {
        0.82, -0.69, 0.80, 0.93, 0.25, 0.47, -0.75, 0.98, 0.24, -0.15, 0.01,
        0.84, 0.68, 0.10, -0.96, -0.26, -0.65, -0.97, -0.03, -0.64, 0.15, -0.43,
        -0.88, -0.90, 0.62, 0.05, -0.92, -0.09, 0.65, -0.76      
  };

  int outputs[] = {
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  };

  int patternCount = sizeof(x) / sizeof(int);

  float weights[2];
  weights[0] = randomFloat();
  weights[1] = randomFloat();

  printf("%f\n", weights[1]);

  float learningRate = 0.1;

  system("PAUSE");
  return 0;
}

I wrote this function to get a pseudo random float between 0 .. 1 inclusive:

float randomFloat()
{
      float r = (float)rand()/(float)RAND_MAX;
      return r;
}

However, it is always returning 0.563585. The same number no matter how many times I run my console application.

EDIT:

Here is my entire application if needed:

#include <stdio.h>
#include <stdlib.h>

float randomFloat()
{
      float r = (float)rand() / (float)RAND_MAX;
      return r;
}

int main(int argc, char *argv[])
{
  float x[] = {
        0.72, 0.91, 0.46, 0.03, 0.12, 0.96, 0.79, 0.46, 0.66, 0.72, 0.35, -0.16,
        -0.04, -0.11, 0.31, 0.00, -0.43, 0.57, -0.47, -0.72, -0.57, -0.25,
        0.47, -0.12, -0.58, -0.48, -0.79, -0.42, -0.76, -0.77
  };

  float y[] = {
        0.82, -0.69, 0.80, 0.93, 0.25, 0.47, -0.75, 0.98, 0.24, -0.15, 0.01,
        0.84, 0.68, 0.10, -0.96, -0.26, -0.65, -0.97, -0.03, -0.64, 0.15, -0.43,
        -0.88, -0.90, 0.62, 0.05, -0.92, -0.09, 0.65, -0.76      
  };

  int outputs[] = {
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  };

  int patternCount = sizeof(x) / sizeof(int);

  float weights[2];
  weights[0] = randomFloat();
  weights[1] = randomFloat();

  printf("%f\n", weights[1]);

  float learningRate = 0.1;

  system("PAUSE");
  return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

油焖大侠 2024-08-17 20:56:41

需要调用

srand(time(NULL));

第一次使用rand之前 。 time 位于 中。

编辑:正如 Jonathan Leffler 指出的那样,这很容易预测,所以不要尝试将其用于密码学。

You need to call

srand(time(NULL));

before using rand for the first time. time is in <time.h>.

EDIT: As Jonathan Leffler points out, this is easily predicted, so don't try using it for cryprography.

耶耶耶 2024-08-17 20:56:41

这是因为C 随机生成器是一个随机生成器(顺便说一句,这是一个非常好的东西,有很多应用程序)。 (ANSI C 标准需要这样的伪随机生成器)

基本上,每次重新启动控制台应用程序时,它都会为随机生成器使用相同的[默认]种子

通过添加像这样的行,

  srand(time(NULL));

每次都会得到一个不同的种子,并且产生的数字序列也会相应变化。
注意:您只需调用 srand() 一次,而不是在每次调用 rand() 之前。

编辑:(测试/调试时提示)
[摘自Artelius的评论]
出于测试目的,最好进行 srand(SOME_CONST),并在运行之间更改常量的值。这样,如果由于某种随机数组合而出现错误,您将能够重现它。

That is because the C random-generator is a pseudo-random generator (and that is, BTW, a very good thing, with many applications). (The ANSI C standard requires such a pseudo-random generator)

Essentially each time your console application is re-started it uses the same [default] seed for the random generator.

by adding a line like

  srand(time(NULL));

you will get a distinct seed each time, and the sequence of number produced with vary accordingly.
Note: you only need to call srand() once, not before each time you call rand().

Edit: (Test/Debug-time hint)
[from a remark by Artelius]
For testing purposes, it's best to srand(SOME_CONST), and change the value of the constant between runs. This way if a bug manifests itself due to some combination of random numbers, you'll be able to reproduce it.

回忆躺在深渊里 2024-08-17 20:56:41

drand48 非常适合您的使用,比 (float)rand( )/RAND_MAX,因为

drand48()erand48() 函数返回非负双精度浮点值,均匀分布在区间 [0.0 , 1.0] 上.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
    int i;

    srand48(time(NULL));

    for (i = 0; i < 10; i++)
        printf("%g\n", drand48());

    return 0;
}

drand48 is perfect for your usage, better than (float)rand()/RAND_MAX, because

The drand48() and erand48() functions return non-negative, double-precision, floating-point values, uniformly distributed over the interval [0.0 , 1.0].

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
    int i;

    srand48(time(NULL));

    for (i = 0; i < 10; i++)
        printf("%g\n", drand48());

    return 0;
}
梦情居士 2024-08-17 20:56:41

您必须初始化一个随机种子,

void srand ( unsigned int seed );

您可以以系统时间为例。

You have to initialize a random seed with

void srand ( unsigned int seed );

You can take for example the system time.

静赏你的温柔 2024-08-17 20:56:41

您是否多次调用它? rand() 将始终返回相同的伪随机数。您可能想使用一些好的种子(例如当前时间)来调用 srand()。

Do you call it more than once? rand() will always return the same pseudo random. You might want to call srand() with some nice seed, like the current time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文