From: James Kanze on 10 Dec 2005 16:35 Carlos Moreno wrote: > gerg wrote: >>rand() has a real crappy range{0..32767} so using something >>else is a good idea. > Errr, no. (I mean, yes, rand() is crappy, but for other > reasons :-)) Errr, no. rand() might be crappy, but the standard doesn't require it. > RAND_MAX is implementation-dependent -- on my platform, it is > 2^31 - 1 (approx. 2.1 billion) And even if it is a lot smaller, that doesn't necessarily mean that the engine behind it is bad (just as the fact that it is big doesn't mean that the engine behind it is good). About all you can really say about rand() is that it depends on the implementation. The standard makes effectively no significant requirements concerning it. -- James Kanze mailto: james.kanze(a)free.fr Conseils en informatique orient?e objet/ Beratung in objektorientierter Datenverarbeitung 9 pl. Pierre S?mard, 78210 St.-Cyr-l'?cole, France +33 (0)1 30 23 00 34 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: sposes on 10 Dec 2005 16:38 I thank everyone for your help. The solution by peter seemed to work best for my aplication, I have however seen Boost before and havent gotten into it yet, but will definately be checking it out in more detail as i learn more. Didnt realize it had its own random number generators. Though I had forgoten to mention my platform is Win32 (WinXP). My particular program was an experiment by me to implement a program by makeing a problem statement, followed by pusedo code algorithm, and then the code. I was writeing an aplication that would generate a set of values that when averaged equal to the users desired average, and the number of values to generate was also user input. Thanks to everyones help I was able to complete the program. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Carlos Moreno on 11 Dec 2005 11:23 sposes wrote: > My particular program was an experiment by me to implement a program by > makeing a problem statement, followed by pusedo code algorithm, and > then the code. I was writeing an aplication that would generate a set > of values that when averaged equal to the users desired average, and > the number of values to generate was also user input. Thanks to > everyones help I was able to complete the program. Ok. Then you should be more than fine with either rand() or rand48(). The reason I asked is that in certain applications, *unpredictability* of the numbers as well as lack of structure/patterns are important. In those cases, rand() and rand48() are completely out of the question, so it's important to us to have an idea of what you were trying to do with the random numbers to be sure that we could give you reasonable advice. Cheers, Carlos -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Diego Martins on 12 Dec 2005 14:33 what about: double zero_one_rand() { double r = static_cast<double>(rand()) / rand(); return r <= 1 ? r : 1/r; } ;-) [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: kanze on 13 Dec 2005 18:03 Diego Martins wrote: > what about: > double zero_one_rand() > { > double r = static_cast<double>(rand()) / rand(); > return r <= 1 ? r : 1/r; > } Would this result in a linear distribution? (I'm too lazy to do the analysis, but a priori, I'm very suspicious of anything that involves division by rand().) It also fails to meet the requirements if rand() returns the same value twice in a row. Not to mention a small problem if the second call to rand() returns 0. (Did I mention that I'm suspicious of this sort of division?) -- James Kanze GABI Software Conseils en informatique orient?e objet/ Beratung in objektorientierter Datenverarbeitung 9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Portable way to write binary data Next: Tree container library |