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...
- Carlos Santander (4/47) Apr 11 2006 --
- Charles (4/49) Apr 11 2006 Ahh thanks!
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
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Charles schrieb am 2006-04-11:Charles wrote: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-----How do i get a number in the range -0.5 to 0.5 ?
Apr 11 2006
In article <pjctg3-8g7.ln1 birke.kuehne.cn>, Thomas Kuehne says...-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Charles schrieb am 2006-04-11: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? -KramerCharles wrote: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-----How do i get a number in the range -0.5 to 0.5 ?
Apr 11 2006
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? -KramerGood 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
In article <e1h555$31cs$1 digitaldaemon.com>, pragma says...In article <e1gu8s$2pnj$1 digitaldaemon.com>, Kramer says...Just what I was looking for. Thanks. -KramerForgive 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? -KramerGood 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
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?-Kramersize_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
Charles wrote:Charles wrote:That's because std.random.rand's range is 0 to uint.max, not 0 to int.max.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 ).<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 asking for. Stewart.
Apr 16 2006
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
pragma escribió:In article <e1gfcb$2a7e$1 digitaldaemon.com>, Charles says...In fact, there's no need to: std.random ctor does that already.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-- Carlos Santander Bernal
Apr 11 2006
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