| | |
need help, need to generate 10 random numbers, such that their sum is less than 1
![]() |
•
•
Join Date: Dec 2006
Posts: 3
Reputation:
Solved Threads: 0
need help, need to generate 10 random numbers, such that their sum is less than 1
0
#1 Dec 18th, 2006
•
•
Join Date: Dec 2006
Posts: 3
Reputation:
Solved Threads: 0
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:
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:
C Syntax (Toggle Plain Text)
#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; } }
Last edited by ~s.o.s~; Dec 18th, 2006 at 1:33 pm. Reason: Added code tags learn to use them yourself.
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
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
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
0
#4 Dec 18th, 2006
•
•
Join Date: Dec 2006
Posts: 3
Reputation:
Solved Threads: 0
Re: need help, need to generate 10 random numbers, such that their sum is less than 1
0
#5 Dec 18th, 2006
•
•
•
•
In addition to what above poster is saying, you need to seed the random number generator using srand().
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
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
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:
C Syntax (Toggle Plain Text)
for (int i = 0; i < 10; i++) sum = sum + (0.1 * (rand() / ((double)RAND_MAX + 1)));
Regards Niek
Re: need help, need to generate 10 random numbers, such that their sum is less than 1
0
#7 Dec 19th, 2006
•
•
•
•
This way you would be sure of the fact that the sum of 10 times this statement can never be more then 1.

@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:
- 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).
- Generate 9 such random numbers and at the last iteration check whether the sum is greater than 0.7.
- If yes then continue with normal processing for the 10th element.
- If no, then set the sum to 0.7.
- 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.
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
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
![]() |
Similar Threads
- Program will not generate random numbers (C++)
- Trying to find a better way to generate random numbers in Dev Pascal (Pascal and Delphi)
- random numbers all different??? (C++)
Other Threads in the C Forum
- Previous Thread: Help in Recursion Assigment.
- Next Thread: Can anyone help me with my c language homework please?
| Thread Tools | Search this Thread |
* adobe ansi api array arrays binarysearch calculate centimeter char character cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. lowest match matrix microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf performance posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h windowsapi






