## digitalmars.D.learn - std.random

• Charles (1/1) Apr 11 2006 How do i get a number in the range -0.5 to 0.5 ?
• Charles (16/17) Apr 11 2006 To Clarify:
• Thomas Kuehne (16/18) Apr 11 2006 -----BEGIN PGP SIGNED MESSAGE-----
• Kramer (5/23) Apr 11 2006 Forgive me for asking such a naive (I feel I should know this already) q...
• pragma (10/14) Apr 11 2006 Good question.
• Kramer (4/22) Apr 11 2006 Just what I was looking for.
• Frank Benoit (5/9) Apr 12 2006 size_t is an _unsigned_ value which can hold a size.
• Stewart Gordon (7/23) Apr 16 2006
• pragma (32/33) Apr 11 2006 Unfortunately, std.random doesn't do anything outside of uint for random...
Charles <noone nowhere.com> writes:
```How do i get a number in the range -0.5 to 0.5 ?
```
Apr 11 2006
Charles <noone nowhere.com> writes:
```Charles wrote:
How do i get a number in the range -0.5 to 0.5 ?

To Clarify:

How do i get a random real in the range -0.5 to 0.5 using std.random ?

--

Even using the traditional C method , using std.c.stdlib, using int.max
for RAND_MAX scince its not defined is not working ->

const int RAND_MAX = int.max;
float x = (rand() / (RAND_MAX + 1.0)) - 0.5;

This always yeilds numbers of ( -0.4999XX , where XX is from 80 99 ).

This code works as expected with DMC.

--

I have a workaround for now using the undocumented std.c.stdlib
random(X) function with

return (random(50000) / 50000.0 ) - 0.5;

but would like to use D's std.random .

Charlie
```
Apr 11 2006
Thomas Kuehne <thomas-dloop kuehne.cn> writes:
```-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Charles schrieb am 2006-04-11:
Charles wrote:
How do i get a number in the range -0.5 to 0.5 ?

const float RAND_MAX = uint.max;
float sum = 0.0;
for(size_t i = 1; i < 101; i++){
float x = (rand() / RAND_MAX) - 0.5;
sum += x;
writefln("round:%s\t%s\t(%s)", i, x, sum / i);
}

Thomas

-----BEGIN PGP SIGNATURE-----

iD8DBQFEPAMv3w+/yD4P9tIRAvPEAJ42zaytStz2jn5BhwkB3YAM3D4B0ACgiDDk
=xxyV
-----END PGP SIGNATURE-----
```
Apr 11 2006
Kramer <Kramer_member pathlink.com> writes:
```In article <pjctg3-8g7.ln1 birke.kuehne.cn>, Thomas Kuehne says...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Charles schrieb am 2006-04-11:
Charles wrote:
How do i get a number in the range -0.5 to 0.5 ?

const float RAND_MAX = uint.max;
float sum = 0.0;
for(size_t i = 1; i < 101; i++){
float x = (rand() / RAND_MAX) - 0.5;
sum += x;
writefln("round:%s\t%s\t(%s)", i, x, sum / i);
}

Thomas

-----BEGIN PGP SIGNATURE-----

iD8DBQFEPAMv3w+/yD4P9tIRAvPEAJ42zaytStz2jn5BhwkB3YAM3D4B0ACgiDDk
=xxyV
-----END PGP SIGNATURE-----

Forgive me for asking such a naive (I feel I should know this already) question
and taking this off-topic, but what is the type size_t?  I've seen it used (and
also ptrdiff_t I believe), but I don't know what they're for or how to use them?

-Kramer
```
Apr 11 2006
pragma <pragma_member pathlink.com> writes:
```In article <e1gu8s\$2pnj\$1 digitaldaemon.com>, Kramer says...
Forgive me for asking such a naive (I feel I should know this already) question
and taking this off-topic, but what is the type size_t?  I've seen it used (and
also ptrdiff_t I believe), but I don't know what they're for or how to use them?

-Kramer

Good question.

The names are inherited from the C/C++ conventions, and serve to separate the
rigidly defined byte-width of D types from platform/architecture definitions for
an address, buffer size or integer.  Using these types, along with D's
references and pointers will ensure that the same code written under a 32-bit
platform will compile just fine under a 64-bit one and so forth (its a
portability thing).

http://www.devx.com/tips/Tip/13388

- EricAnderton at yahoo
```
Apr 11 2006
Kramer <Kramer_member pathlink.com> writes:
```In article <e1h555\$31cs\$1 digitaldaemon.com>, pragma says...
In article <e1gu8s\$2pnj\$1 digitaldaemon.com>, Kramer says...
Forgive me for asking such a naive (I feel I should know this already) question
and taking this off-topic, but what is the type size_t?  I've seen it used (and
also ptrdiff_t I believe), but I don't know what they're for or how to use them?

-Kramer

Good question.

The names are inherited from the C/C++ conventions, and serve to separate the
rigidly defined byte-width of D types from platform/architecture definitions for
an address, buffer size or integer.  Using these types, along with D's
references and pointers will ensure that the same code written under a 32-bit
platform will compile just fine under a 64-bit one and so forth (its a
portability thing).

http://www.devx.com/tips/Tip/13388

- EricAnderton at yahoo

Just what I was looking for.

Thanks.

-Kramer
```
Apr 11 2006
Frank Benoit <benoit__ __tionex.de> writes:
``` Forgive me for asking such a naive (I feel I should know this already) question
and taking this off-topic, but what is the type size_t?  I've seen it used (and
also ptrdiff_t I believe), but I don't know what they're for or how to use
them?

-Kramer

size_t is an _unsigned_ value which can hold a size.
ptrdiff_t is a _signed_ value which can hold the offset between two
pointers.

These aliases are used to increase portability.
I know them mainly from C/C++.
```
Apr 12 2006
Stewart Gordon <smjg_1998 yahoo.com> writes:
```Charles wrote:

Charles wrote:

How do i get a number in the range -0.5 to 0.5 ?

To Clarify:

How do i get a random real in the range -0.5 to 0.5 using std.random ?

--

Even using the traditional C method , using std.c.stdlib, using int.max
for RAND_MAX scince its not defined is not working ->

That's because std.random.rand's range is 0 to uint.max, not 0 to int.max.

const int RAND_MAX = int.max;
float x = (rand() / (RAND_MAX + 1.0)) - 0.5;

This always yeilds numbers of ( -0.4999XX , where XX is from 80 99 ).

<snip>

Very weird.  When I try it, I get the range -0.5 to 1.5 as expected.

If OTOH I use uint.max instead of RAND_MAX, then I get what you're

Stewart.
```
Apr 16 2006
pragma <pragma_member pathlink.com> writes:
```In article <e1gfcb\$2a7e\$1 digitaldaemon.com>, Charles says...
How do i get a number in the range -0.5 to 0.5 ?

Unfortunately, std.random doesn't do anything outside of uint for randomness (as
you've probably already learned).  This doesn't leave you completely out of
luck.

Since the range returned by rand() is distributed over 0 to uint.max, all you
need to do is map this range to the range of floating point values you want.

/**/ double result = ((cast(double)rand())/uint.max); // result = [0..1]
/**/ result = result -0.5 // shift the range down by 0.5

Remember to call rand_seed(), as this kicks things off with the random number
generator.  The index parameter is useful for replaying simulations or games,
when you want to re-create the sequence of random values returned by rand() (its
deterministic after all, and not truely random).

Fot that I'd reccomend using the current time for seed and 0 as the index.  The
std.date package has a function that will work great for the time, but we need
to truncate it to a uint as it's too big (long).

/**/ rand_seed(cast(uint)getUTCTime,0); // use cast() to truncate

For completeness, here is a complete listing that displays 20 random numbers:

/**/ import std.random;
/**/ import std.date;
/**/ import std.stdio;
/**/
/**/ void main(){
/**/ 	rand_seed(cast(uint)getUTCtime(),0);
/**/
/**/ 	for(int i=0; i<20; i++){
/**/ 		double result = ((cast(double)rand())/uint.max);
/**/ 		result = result - 0.5;
/**/ 		writefln("value: %f",result);
/**/ 	}
/**/ }

Enjoy!

- EricAnderton at yahoo
```
Apr 11 2006
Carlos Santander <csantander619 gmail.com> writes:
```pragma escribió:
In article <e1gfcb\$2a7e\$1 digitaldaemon.com>, Charles says...
How do i get a number in the range -0.5 to 0.5 ?

Unfortunately, std.random doesn't do anything outside of uint for randomness
(as
you've probably already learned).  This doesn't leave you completely out of
luck.

Since the range returned by rand() is distributed over 0 to uint.max, all you
need to do is map this range to the range of floating point values you want.

/**/ double result = ((cast(double)rand())/uint.max); // result = [0..1]
/**/ result = result -0.5 // shift the range down by 0.5

Remember to call rand_seed(), as this kicks things off with the random number
generator.  The index parameter is useful for replaying simulations or games,
when you want to re-create the sequence of random values returned by rand()
(its
deterministic after all, and not truely random).

In fact, there's no need to: std.random ctor does that already.

Fot that I'd reccomend using the current time for seed and 0 as the index.  The
std.date package has a function that will work great for the time, but we need
to truncate it to a uint as it's too big (long).

/**/ rand_seed(cast(uint)getUTCTime,0); // use cast() to truncate

For completeness, here is a complete listing that displays 20 random numbers:

/**/ import std.random;
/**/ import std.date;
/**/ import std.stdio;
/**/
/**/ void main(){
/**/ 	rand_seed(cast(uint)getUTCtime(),0);
/**/
/**/ 	for(int i=0; i<20; i++){
/**/ 		double result = ((cast(double)rand())/uint.max);
/**/ 		result = result - 0.5;
/**/ 		writefln("value: %f",result);
/**/ 	}
/**/ }

Enjoy!

- EricAnderton at yahoo

--
Carlos Santander Bernal
```
Apr 11 2006
Charles <noone nowhere.com> writes:
```Ahh thanks!

I was missing the cast(double) for the rand() call , and there were all
coming out -0.5!

pragma wrote:
In article <e1gfcb\$2a7e\$1 digitaldaemon.com>, Charles says...

How do i get a number in the range -0.5 to 0.5 ?

Unfortunately, std.random doesn't do anything outside of uint for randomness
(as
you've probably already learned).  This doesn't leave you completely out of
luck.

Since the range returned by rand() is distributed over 0 to uint.max, all you
need to do is map this range to the range of floating point values you want.

/**/ double result = ((cast(double)rand())/uint.max); // result = [0..1]
/**/ result = result -0.5 // shift the range down by 0.5

Remember to call rand_seed(), as this kicks things off with the random number
generator.  The index parameter is useful for replaying simulations or games,
when you want to re-create the sequence of random values returned by rand()
(its
deterministic after all, and not truely random).

Fot that I'd reccomend using the current time for seed and 0 as the index.  The
std.date package has a function that will work great for the time, but we need
to truncate it to a uint as it's too big (long).

/**/ rand_seed(cast(uint)getUTCTime,0); // use cast() to truncate

For completeness, here is a complete listing that displays 20 random numbers:

/**/ import std.random;
/**/ import std.date;
/**/ import std.stdio;
/**/
/**/ void main(){
/**/ 	rand_seed(cast(uint)getUTCtime(),0);
/**/
/**/ 	for(int i=0; i<20; i++){
/**/ 		double result = ((cast(double)rand())/uint.max);
/**/ 		result = result - 0.5;
/**/ 		writefln("value: %f",result);
/**/ 	}
/**/ }

Enjoy!

- EricAnderton at yahoo

```
Apr 11 2006