Random float number
Solution 1
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.
Solution 2
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.
Solution 3
drand48 is perfect for your usage, better than (float)rand()/RAND_MAX
, because
The
drand48()
anderand48()
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;
}
Solution 4
You have to initialize a random seed with
void srand ( unsigned int seed );
You can take for example the system time.
Solution 5
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.
Richard Knop
I'm a software engineer mostly working on backend from 2011. I have used various languages but has been mostly been writing Go code since 2014. In addition, I have been involved in lot of infra work and have experience with various public cloud platforms, Kubernetes, Terraform etc. For databases I have used lot of Postgres and MySQL but also Redis and other key value or document databases. Check some of my open source projects: https://github.com/RichardKnop/machinery https://github.com/RichardKnop/go-oauth2-server https://github.com/RichardKnop
Updated on September 23, 2020Comments
-
Richard Knop over 3 years
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; }
-
Artelius over 14 yearsFor 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.
-
mjv over 14 years@artelius, excellent suggestion. I added this as a note to the response itself!
-
Jonathan Leffler over 14 yearsBe careful with using time(); it is rather easily predictable and hence your random sequences can be predicted too. It may not matter in this case, but be careful in general.
-
Jonathan Leffler over 14 years@artelius and mjv: it is a good point; it can also be a good idea to allow the user to specify the seed on the command line instead of using time(), and it can often (but not always) be a good idea to report the seed that was used (whether from the command line or any other source) for working with the results.