0

my output contains 3 textfields where when given N value (the doClick() function clicks 3 textfields automatically), then it randomly generates 3 numbers in the three textfields , My code is generating only random numbers , but i want those randomly generated numbers to be exactly add up to given N.

example : when N=20 then possible answers can be:

1.10,10,0 i.e. (textfield1 show 10, textfield2 show 10, textfiled3 show 0, whose sum adds up to given N )

2.15,3,2

3.10,5,5

random numbers can be any positive integer but it should add up to given N.

Any help please.

prasanna
  • 13
  • 2
  • In what way does your edit change this from being a question that has already been answered? – pjs Nov 04 '21 at 16:37
  • there is no change in my question, and i am expecting some other answers because the provided one didn't worked as desired. – prasanna Nov 05 '21 at 06:17
  • [There won't be any new answers unless your question is reopened, and that won't happen unless you make an edit which convinces enough qualified voters that your question is different from the one linked in the closure notice](https://stackoverflow.com/help/closed-questions). – pjs Nov 05 '21 at 14:55
  • thank you for the information. – prasanna Nov 05 '21 at 16:27
  • That's a very odd choice you've made, to state in a comment that the answer given below didn't work but then to check it off as the solution! You should follow the link to the question you've duplicated, you'll find much better answers there. Consider upvoting one or more of those if you find them useful. – pjs Nov 05 '21 at 17:42
  • The approach given [here](https://stackoverflow.com/a/31814348/2166798) is efficient, generalizes trivially to more than 3 values, and yields uniformly distributed values. Substitute simply generating random numbers in the range where it currently does sample (without replacement) if you want zeros as a possibility. Should be straightforward to translate from python to java. – pjs Nov 12 '21 at 14:21

1 Answers1

0

You could use Math.random() instead of the Random class. Math.random() returns a double between 0 and 1. So the only thing you have to do is multiply the result of Math.random() with N. The next number would be N minus your result from the subtraction of N and the previous result.

final int N = 20;
final int result0 = (int) (Math.random() * N);
final int result1 = (int) (Math.random() * (N - result0));
final int result2 = N - result0 - result1;

EDIT: You could than choose the first, second and third parameter randomly too.

Stephan
  • 42
  • 5
  • 1
    The results will be a decreasing sequence in expectation, with a much greater chance of 0's towards the end (and hence fairly predictable/non-random). – pjs Nov 04 '21 at 15:23
  • He could choose the first, second and third parameter randomly too. Since the equation is x + y + z = N, he could also build a table with all possible parameters. and than choose the table entry randomly. But with my edit it should do the trick for him. – Stephan Nov 12 '21 at 09:56