Board index » cppbuilder » random numbers between 0 and 1

random numbers between 0 and 1


2005-02-09 01:05:34 AM
cppbuilder103
I'm trying to put a simple idea into code, but I keep getting syntactic
errors of one kind or another. The condition I'm trying to code is, in
English:
IF the absolute value of the difference between two random numbers
between 0 and 1 is smaller than a value Maxx, THEN ...
Things I've tried;
float Maxx; if (abs(random-random) < Maxx) ...
float a, b, Maxx; a = random; b = random; if (abs(a-b) < Maxx) ...
Gerrit.
 
 

Re:random numbers between 0 and 1

On 08 Feb 2005, Gerrit boldly opined:
Quote
I'm trying to put a simple idea into code, but I keep getting syntactic
errors of one kind or another. The condition I'm trying to code is, in
English:

IF the absolute value of the difference between two random numbers
between 0 and 1 is smaller than a value Maxx, THEN ...

Things I've tried;

float Maxx; if (abs(random-random) < Maxx) ...

float a, b, Maxx; a = random; b = random; if (abs(a-b) < Maxx) ...
Oh, in both cases Maxx is initialised, as in float Maxx = 0.4; or some
such value.
G.
 

Re:random numbers between 0 and 1

"Gerrit" < XXXX@XXXXX.COM >wrote in message
Quote
On 08 Feb 2005, Gerrit boldly opined:

>I'm trying to put a simple idea into code, but I keep getting syntactic
>errors of one kind or another. The condition I'm trying to code is, in
>English:
>
>IF the absolute value of the difference between two random numbers
>between 0 and 1 is smaller than a value Maxx, THEN ...
>
>Things I've tried;
>
>float Maxx; if (abs(random-random) < Maxx) ...
>
>float a, b, Maxx; a = random; b = random; if (abs(a-b) < Maxx) ...

Oh, in both cases Maxx is initialised, as in float Maxx = 0.4; or some
such value.
Aside from suggesting that you use double instead of float,
you'll need to post some actual code and describe the syntax
errors. From the code that you show, you're testing abs(0) < Maxx.
 

{smallsort}

Re:random numbers between 0 and 1

As Duane already said, i will also suggest to use double instead of float.
Also notice that the random function returns a int, so to get it in the
range between 0 and 1 you need to do something along the lines of
double RandomNumber = random(Resolution) / Resolution
Also be aware that the abs function is for integers and won't work, you
should use fabs instead when dealing for floating point.
/Palle
 

Re:random numbers between 0 and 1

On 08 Feb 2005, Palle Meinert wrote:
Quote
As Duane already said, i will also suggest to use double instead of
float. Also notice that the random function returns a int, so to get
it in the range between 0 and 1 you need to do something along the
lines of

double RandomNumber = random(Resolution) / Resolution

Also be aware that the abs function is for integers and won't work,
you should use fabs instead when dealing for floating point.
Thanks, Duane and Palle. I'm still thinking in Pascal now and then, where
Random without an argument returns a real between 0 and 1. Choosing a
large value for Resolution will work fine though.
Gerrit
 

Re:random numbers between 0 and 1

Gerrit < XXXX@XXXXX.COM >wrote:
Quote
Things I've tried;

float Maxx; if (abs(random-random) < Maxx) ...

float a, b, Maxx; a = random; b = random; if (abs(a-b) < Maxx) ...
I think that the sorce of your problems s functon "abs"
which returns int values.
Try "fabs" instead - float version of abs.
orphan