Hi,
Would really appreciate if someone can help me.

Need to generate 10 random numbers such that their sum is less than 1.

thanks so much in advance
manu

Sorry, forgot to add my code as per the rules.

And also, missed one part, i want inverse of the sum of random numbers which is less than 1 but greater than 0.75
such as :
sum = 1/23+1/32...
such that sum < 1 and > 0.75

and the problem is that if i use the code below which i was trying, simulator runs for ever and there is no hit.
Need help on this:

#include <cstdlib> 
#include <ctime> 
#include <iostream>

using namespace std;

int main()
{
    int not1= 0;
   double sum = 0;
   while (!not1)
   {
      for (int i = 0; i < 12; i ++)
      {
       sum = sum + (1/  (((rand()%1214) + 1));
      }
   if ((sum <=1) && (sum > .75))
   not1 = 1;
  }
}

Hi,
Would really appreciate if someone can help me.

Need to generate 10 random numbers such that their sum is less than 1.

thanks so much in advance
manu

1) Why do you use not1, when you could just use sum <= 1.0 && sum > 0.75 for the where loop condition?

2) Do you need to record or output the random numbers you're summing?

3) What happens if the sum is over 1 before you have 10 numbers? Do you start throwing them out? Stop with a shorter list?

4) You have a paren mismatch. I'll let you fix it.

5) If you're looking for 10 numbers, why do you have i<12 ?

In addition to what above poster is saying, you need to seed the random number generator using srand().

In addition to what above poster is saying, you need to seed the random number generator using srand().

Thanks so much for your replies:

Infarction, actually, i just wrote a sample code that i was trying.
So, sorry for the typos, yes its <10 instead of <12.

# If the sum gets over 1 before i use all the ten numbers then this would be invalid case.

I have to make sure that all 10 numbers are used and that no number is given a value of 0

# I will be saving random numbers that i am generating.
# Ok, i will not use not1 in while loop, instead will use the condition itself

MAIN PROBLEM NOW IS THAT SIMULATOR RUNS FOR EVER BECAUSE THIS CONDITION IS NOT BEING MET
sum < 1 && sum > 0.75

So, if someone has better idea of how this can be done in small time.

Thanks
manu

MAIN PROBLEM NOW IS THAT SIMULATOR RUNS FOR EVER BECAUSE THIS CONDITION IS NOT BEING MET
sum < 1 && sum > 0.75

No reason for yelling... ;) If you put a printf() in your loop you would see what the output from your srand() is and check WHY the if statement wasn't true.
If I had to make this program I would use something like:

for (int i = 0; i < 10; i++)
    sum = sum + (0.1 * (rand() / ((double)RAND_MAX + 1)));

This way you would be sure of the fact that the sum of 10 times this statement can never be more then 1.

Edited 6 Years Ago by Nick Evan: n/a

This way you would be sure of the fact that the sum of 10 times this statement can never be more then 1.

..and neither would the sum be more than 0.7 ;)

@manu124:

I sure hope you understand the implications of the statement you are trying to put up. You want a random generation program but still want the sum to be deterministic that is less than 10 and more than 7. How do you think that could be done without actually cheating....;)

My idea:

  1. Start off by generating numbers between 0.01 and 0.1 ( since you haven't mentioned the precision you require, I will safely assume these two values).
  2. Generate 9 such random numbers and at the last iteration check whether the sum is greater than 0.7.
  3. If yes then continue with normal processing for the 10th element.
  4. If no, then set the sum to 0.7.
  5. Generate a new random number between 0 and 0.3 with whatever precision you like eg. 0.1, 0.01 etc (0.3 since 1 - 0.7 = 0.3 ) and add this to the new sum.

ok if I understand you right, you want the sum of all your numbers to be less than 1, and you want the sum of the reciprocals to be greater than 0.75? If so...

Well this is cheating, but you can force the results out by picking any 10 pseudo random numbers (all greater than or equal to 1). Sum the product of these numbers

for example, suppose my numbers were:
1,2,3,4,5,6,7,8,9,10
so,
sum=1+2+3+4+5+6+7+8+9+10=55
now we are going to create a scalar factor to shrink the numbers
scalar_factor = 1-sum/(sum+1);

now multiply all your numbers by this scalar_factor
1 would become 0.01786
2 would become 0.03572
.
.
.
9 would become 0.16074
10 would become 0.17860

and that should give your new 10 number, just go ahead and double check it meets your 0.75 criteria. I'm to lazy to create any bounds to guarantee you a minimum of 0.75 but you get the idea

This article has been dead for over six months. Start a new discussion instead.