Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
April 11, 2006 std.random | ||||
---|---|---|---|---|
| ||||
How do i get a number in the range -0.5 to 0.5 ? |
April 11, 2006 Re: std.random | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charles | 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
|
April 11, 2006 Re: std.random | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charles Attachments: | 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
|
April 11, 2006 Re: std.random | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charles | 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 |
April 11, 2006 Re: std.random | ||||
---|---|---|---|---|
| ||||
Posted in reply to Thomas Kuehne | 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 >8wfG8fdPa8ijUaDOVbdHW3g= >=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 |
April 11, 2006 Re: std.random | ||||
---|---|---|---|---|
| ||||
Posted in reply to pragma | 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 |
April 11, 2006 Re: std.random | ||||
---|---|---|---|---|
| ||||
Posted in reply to pragma | 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
|
April 11, 2006 Re: std.random | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kramer | 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 |
April 11, 2006 Re: std.random | ||||
---|---|---|---|---|
| ||||
Posted in reply to pragma | 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 |
April 12, 2006 Re: std.random | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kramer | > 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++. |
Copyright © 1999-2021 by the D Language Foundation