Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Say, I have a total number of 85. you have to divide it into 3 individual numbers (where a+b+c = 85) and 0<=a<=40 45; 0<=b<=45; 0=<c<15.< b="">

Can you help me to solve it?


It's a problem with a random function that has an exact range to select with a condition.

What I have tried:

Tried in C++ and python.

Say, I have a total number of 85. you have to divide it into 3 individual numbers (where a+b+c = 85) and 0<=a<=40 45; 0<=b<=45; 0=<c<15.
Can you help me to solve it?


It's a problem with a random function that has an exact range to select with a condition.
Posted
Updated 20-Oct-21 22:32pm
v2
Comments
Patrice T 21-Oct-21 2:45am    
Show your code to get help.

You know, you cannot random pick the numbers in their allowed ranges. For instance, you cannot choose a=0, because then no pair of {b,c} values could fullfill the requirement
a + b + c = 85
You can however, freely random pick a in the [25,40] range.
Once you've got a, you may random pick b in its current allowed range [85-15-a, 45] (once again, if b < (85-15-a) then no value of c would be good for obtaining a+b+c=85).
Finally you compute c = 85-a-b.
 
Share this answer
 
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
[no name] 22-Oct-21 11:28am    
Thank you for your explanation. But it is not included my grade nor any paid work. It is just a common thing that I am trying to learn about the way how we could randomly pick some number in a complex problem.
Why use random numbers? There are not that many combinations so an iterative approach is simple. You have three values, each with their ranges defined. I would make three for loops that iterate through those values and evaluate the sum of them for each iteration. Test each sum to see if it matches and write out the values when it does.

That's the basic algorithm. Since this is your homework, you get to write the code.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900