0

I need to produces numbers between 0 and a max (seen in code as assetMax). In the code, the rand()/RAND_MAX always produces 0 and I cannot seem to figure out why. I use the rand() function immediately before it to produce values in a range and it works completely fine. However, here it does not.

I have tried to switch the order of the variables, create the random number in a separate double before multiplying the two, and the header.

void cPortfolio::randomize(cProblem &portfolioProblem) {

  int assetCount = 6 * rand() / RAND_MAX + (portfolioProblem.assetMax-8); //this line works as expected
  int test; 
  for (int i = 0; i < assetCount; i++) {
      double num = rand() / RAND_MAX;  //this always produces 0.0000
      int test = num * (portfolioProblem.assetNum);   } `} //cannot format these correctly please ignore the brackets
abi.ravi0901
  • 101
  • 3
  • 2
    The `rand` function returns an integer, and `RAND_MAX` is an integer. Integer division result in an integer, any decimals are truncated (dropped). That means if `rand` returns *any* value except `RAND_MAX` the integer result will be zero, and if `rand` returns `RAND_MAX` the result will be `1`. – Some programmer dude Aug 09 '19 at 19:50
  • 3
    do. not. use. `rand()`. in. C++. C++ has proper randomness features, use those instead. – spectras Aug 09 '19 at 19:51
  • Right, but I am always getting 0 as that integer even when i have rand() /RAND_MAX * (portfolioProblem.assetNum) – abi.ravi0901 Aug 09 '19 at 19:52
  • 1
    **WARNING**: Using [`rand()` is highly problematic](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) and you’re strongly encouraged to use an appropriate [random number generator facility in the Standard Library](http://en.cppreference.com/w/cpp/numeric/random) that produces high-quality random values. Your use of `time(NULL)` as a random number seed means that this will produce identical results if run in the same second, and on many platforms `rand()` is [*barely* random at all](http://dilbert.com/strip/2001-10-25). – tadman Aug 09 '19 at 19:52
  • @spectras ok, sorry could you refer me to the name of one such feature – abi.ravi0901 Aug 09 '19 at 19:52
  • 4
    And it doesn't matter if you assign the result to a floating point variable. The division itself will still be an integer division. – Some programmer dude Aug 09 '19 at 19:52
  • `rand()/RAND_MAX` is just going to return garbage because `rand()` is garbage and `RAND_MAX `might be some stupidly low value like 42. (This is only a slight exaggeration, sometimes it's 32767). Fixing this code is not worth the time. Use the C++ Standard Library where this is not an issue because the random generator functions work properly. – tadman Aug 09 '19 at 19:53
  • "Right, but I am always getting 0 as that integer even when i have rand() /RAND_MAX * (portfolioProblem.assetNum)" Because `0 * (portfolioProblem.assetNum)` is 0. – Eljay Aug 09 '19 at 19:59
  • 3
    @tadman — get your facts right. `RAND_MAX` is required to be at least 32767 and is often much larger. – Pete Becker Aug 09 '19 at 19:59
  • 2
    @tadman just because it's easy to do better than `rand()` doesn't make it garbage. It's purely a problem of integer division. And you should be aware of the [pigeon-hole principle](https://en.wikipedia.org/wiki/Pigeonhole_principle) too. – Mark Ransom Aug 09 '19 at 19:59
  • 1
    @PeteBecker there's at least one major compiler that uses `RAND_MAX` of `32767`. Imagine my surprise when I discovered it. – Mark Ransom Aug 09 '19 at 20:01
  • Reopened. The underlying problem is, as in the former duplicate, integer division. But a proper answer is more nuanced. Sigh. Closed again, missing the point. – Pete Becker Aug 09 '19 at 20:11
  • 4
    @PeteBecker We already have targets for how to generate random numbers in a range, and why the OP is getting 0 with integer division. We don't really need another Q&A for this. – NathanOliver Aug 09 '19 at 20:15
  • 1
    Please see [rand() Considered Harmful](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful). – Jesper Juhl Aug 09 '19 at 20:27

0 Answers0