![]() |
| ||
| need help, need to generate 10 random numbers, such that their sum is less than 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 |
| ||
| Re: need help, need to generate 10 random numbers, such that their sum is less than 1 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> Quote:
|
| ||
| Re: need help, need to generate 10 random numbers, such that their sum is less than 1 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? |
| ||
| Re: need help, need to generate 10 random numbers, such that their sum is less than 1 In addition to what above poster is saying, you need to seed the random number generator using srand(). |
| ||
| Re: need help, need to generate 10 random numbers, such that their sum is less than 1 Quote:
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 |
| ||
| Re: need help, need to generate 10 random numbers, such that their sum is less than 1 Quote:
If I had to make this program I would use something like: 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 |
| ||
| Re: need help, need to generate 10 random numbers, such that their sum is less than 1 Quote:
@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:
|
| ||
| Re: need help, need to generate 10 random numbers, such that their sum is less than 1 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 |
| All times are GMT -4. The time now is 12:29 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC