need help, need to generate 10 random numbers, such that their sum is less than 1

Reply

Join Date: Dec 2006
Posts: 3
Reputation: manu124 is an unknown quantity at this point 
Solved Threads: 0
manu124 manu124 is offline Offline
Newbie Poster

need help, need to generate 10 random numbers, such that their sum is less than 1

 
0
  #1
Dec 18th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 3
Reputation: manu124 is an unknown quantity at this point 
Solved Threads: 0
manu124 manu124 is offline Offline
Newbie Poster

Re: need help, need to generate 10 random numbers, such that their sum is less than 1

 
0
  #2
Dec 18th, 2006
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:

  1. #include <cstdlib>
  2. #include <ctime>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int not1= 0;
  10. double sum = 0;
  11. while (!not1)
  12. {
  13. for (int i = 0; i < 12; i ++)
  14. {
  15. sum = sum + (1/ (((rand()%1214) + 1));
  16. }
  17. if ((sum <=1) && (sum > .75))
  18. not1 = 1;
  19. }
  20. }


Originally Posted by manu124 View Post
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
Last edited by ~s.o.s~; Dec 18th, 2006 at 1:33 pm. Reason: Added code tags learn to use them yourself.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,582
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: need help, need to generate 10 random numbers, such that their sum is less than 1

 
0
  #3
Dec 18th, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: need help, need to generate 10 random numbers, such that their sum is less than 1

 
0
  #4
Dec 18th, 2006
In addition to what above poster is saying, you need to seed the random number generator using srand().
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 3
Reputation: manu124 is an unknown quantity at this point 
Solved Threads: 0
manu124 manu124 is offline Offline
Newbie Poster

Re: need help, need to generate 10 random numbers, such that their sum is less than 1

 
0
  #5
Dec 18th, 2006
Originally Posted by sunnypalsingh View Post
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,815
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 296
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: need help, need to generate 10 random numbers, such that their sum is less than 1

 
0
  #6
Dec 19th, 2006
Originally Posted by manu124
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:
  1.  
  2. for (int i = 0; i < 10; i++)
  3. 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.

Regards Niek
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: need help, need to generate 10 random numbers, such that their sum is less than 1

 
0
  #7
Dec 19th, 2006
Originally Posted by niek_e View Post
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.
Last edited by ~s.o.s~; Dec 19th, 2006 at 2:15 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: need help, need to generate 10 random numbers, such that their sum is less than 1

 
0
  #8
Dec 20th, 2006
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC