Generate Random numbers and sum to specific given number Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Generate Random numbers and sum to specific given number

31 Aug 2017, 19:47

Hi all

is it possible to generate random numbers and do simple math, then i can get the final result which i given?

example in the below

given number: 52031.04

random numbers (R):
3264(R)*4.52(R)=14753.28
3264(R)*4.07(R)=13284.48
3264(R)*3.84(R)=12533.76
3312(R)*3.46(R)=11459.52
then add them up
14753.28+13284.48+12533.76+11459.52=52031.04 (the given number)

Note that each random numbers can be close to the average values but no need to be the same
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Generate Random numbers and sum to specific given number

31 Aug 2017, 20:10

I would pick 1 random number (the total, t), and 3 additional random numbers (numbers that form part of the sum: a,b,c).

I would do d := t - a - b - c, to get the 4th 'random' number in the sum.

And thus a+b+c+d=t.

You may find the Format function useful, to write the numbers to 2dp (2 decimal places):

Code: Select all

MsgBox, % Format("{:0.2f}", 2/3) ;0.67
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: Generate Random numbers and sum to specific given number

31 Aug 2017, 21:49

jeeswg wrote:I would pick 1 random number (the total, t), and 3 additional random numbers (numbers that form part of the sum: a,b,c).

I would do d := t - a - b - c, to get the 4th 'random' number in the sum.

And thus a+b+c+d=t.

You may find the Format function useful, to write the numbers to 2dp (2 decimal places):

Code: Select all

MsgBox, % Format("{:0.2f}", 2/3) ;0.67
sorry i didn't make myself clear, the "t" is known value, need to generate the a,b,c,d parts
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Generate Random numbers and sum to specific given number

31 Aug 2017, 21:59

Yes, instead of 'I would pick 1 random number (the total, t)', I should have said 'Given a number (the total, t)', but everything I said still stands.

You are given t, you pick 3 random values: a,b,c, and you do d = t-a-b-c to get the fourth value in the sum: d.

Otherwise what are you going to do? Pick 4 random values forever until you get t? I might try and do some code in a bit ...

[EDIT:] Done.

Code: Select all

q:: ;split a number into 4 'random' parts
vTotal := 52031.04
vRange1 := (vTotal / 4) - 3000
vRange2 := (vTotal / 4) + 3000
vCount := 0
Loop
{
	if (vCount = 10)
		break
	Random, vRand1, % vRange1, % vRange2
	Random, vRand2, % vRange1, % vRange2
	Random, vRand3, % vRange1, % vRange2
	vRand4 := vTotal-vRand1-vRand2-vRand3
	if (vRand4 < 0)
		continue
	vCount++
	vOutput .= Format("{:0.2f}+{:0.2f}+{:0.2f}+{:0.2f}={:0.2f}", vRand1, vRand2, vRand3, vRand4, vTotal) "`r`n"
}
Clipboard := vOutput
MsgBox, % "done"
return
Interesting script idea, do you mind saying what you might use it for? Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: Generate Random numbers and sum to specific given number

01 Sep 2017, 03:31

jeeswg wrote:Yes, instead of 'I would pick 1 random number (the total, t)', I should have said 'Given a number (the total, t)', but everything I said still stands.

You are given t, you pick 3 random values: a,b,c, and you do d = t-a-b-c to get the fourth value in the sum: d.

Otherwise what are you going to do? Pick 4 random values forever until you get t? I might try and do some code in a bit ...

[EDIT:] Done.

Code: Select all

q:: ;split a number into 4 'random' parts
vTotal := 52031.04
vRange1 := (vTotal / 4) - 3000
vRange2 := (vTotal / 4) + 3000
vCount := 0
Loop
{
	if (vCount = 10)
		break
	Random, vRand1, % vRange1, % vRange2
	Random, vRand2, % vRange1, % vRange2
	Random, vRand3, % vRange1, % vRange2
	vRand4 := vTotal-vRand1-vRand2-vRand3
	if (vRand4 < 0)
		continue
	vCount++
	vOutput .= Format("{:0.2f}+{:0.2f}+{:0.2f}+{:0.2f}={:0.2f}", vRand1, vRand2, vRand3, vRand4, vTotal) "`r`n"
}
Clipboard := vOutput
MsgBox, % "done"
return
Interesting script idea, do you mind saying what you might use it for? Cheers.
Thanks for your kindly help, i will try to explain what i'm going to do
i changed some string name, hope you can understand it :)

vTot := 52031.04 := (vRange1+vRange2+vRange3+vRange4)

vRange1 := (vTot / 4) := vTot1 ; this random number(vTot1) could be 3200~3400, then multiply a random number 3.40~4.50(Vstr1)

vRange2 := (vTot / 4):= vTot2 ;this random number(vTot2) could be 3200~3400, then multiply a random number 3.40~4.50(Vstr2)

vRange3 := (vTot / 4) := vTot3 ;this random number(vTot3) could be 3200~3400, then multiply a random number 3.40~4.50(Vstr3)

vRange4 := (vTot / 4):= vTot4 ;this random number(vTot4) could be 3200~3400, then multiply a random number 3.40~4.50(Vstr4)

So let's say we are testing the script, now launch it and put the known number 52031.04 and we will get the results as below:

3264*4.52=14753.28
3264*4.07=13284.48
3264*3.84=12533.76
3312*3.46=11459.52

The results will be ready for use now
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: Generate Random numbers and sum to specific given number

01 Sep 2017, 09:12

jeeswg wrote:Yes, instead of 'I would pick 1 random number (the total, t)', I should have said 'Given a number (the total, t)', but everything I said still stands.

You are given t, you pick 3 random values: a,b,c, and you do d = t-a-b-c to get the fourth value in the sum: d.

Otherwise what are you going to do? Pick 4 random values forever until you get t? I might try and do some code in a bit ...

[EDIT:] Done.

Code: Select all

q:: ;split a number into 4 'random' parts
vTotal := 52031.04
vRange1 := (vTotal / 4) - 3000
vRange2 := (vTotal / 4) + 3000
vCount := 0
Loop
{
	if (vCount = 10)
		break
	Random, vRand1, % vRange1, % vRange2
	Random, vRand2, % vRange1, % vRange2
	Random, vRand3, % vRange1, % vRange2
	vRand4 := vTotal-vRand1-vRand2-vRand3
	if (vRand4 < 0)
		continue
	vCount++
	vOutput .= Format("{:0.2f}+{:0.2f}+{:0.2f}+{:0.2f}={:0.2f}", vRand1, vRand2, vRand3, vRand4, vTotal) "`r`n"
}
Clipboard := vOutput
MsgBox, % "done"
return
Interesting script idea, do you mind saying what you might use it for? Cheers.
from your script, i get the results, i have to admit it was great,
12826.96+10550.94+14240.99+14412.15=52031.04
but, is it possible to do a multiply to get the vRand1 2 3 4?
vRand1 := (Random number 3.40~4.59) * (Random number)
vRand2 := ;as vRand1 doing random stuff
vRand3 := ;as vRand1 doing random stuff
vRand4 := ;as vRand1 doing random stuff
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Generate Random numbers and sum to specific given number

01 Sep 2017, 10:08

jeeswg wrote:vRange1 := (vTotal / 4) - 3000
vRange2 := (vTotal / 4) + 3000
why limit a "random" number to one quarter of vTotal +/- 3000 ??

Code: Select all

vTotal := 52031.04
Loop
{
	vRange := vTotal
	Random, vRand1, 0, % vRange

	vRange -= vRand1
	Random, vRand2, 0, % vRange

	vRange -= vRand2
	Random, vRand3, 0, % vRange

	vRand4 := vTotal - vRand3 - vRand2 - vRand1
	
	if !(vRand1>0 && vRand2>0 && vRand3>0 && vRand4>0)
		continue

	vOutput .= Format("{:0.2f}+{:0.2f}+{:0.2f}+{:0.2f}={:0.2f}", vRand1, vRand2, vRand3, vRand4, vTotal) "`r`n"	
	count ++
	if count = 10
		break
}

MsgBox % vOutput
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: Generate Random numbers and sum to specific given number

01 Sep 2017, 10:42

AlphaBravo wrote:
jeeswg wrote:vRange1 := (vTotal / 4) - 3000
vRange2 := (vTotal / 4) + 3000
why limit a "random" number to one quarter of vTotal +/- 3000 ??

Code: Select all

vTotal := 52031.04
Loop
{
	vRange := vTotal
	Random, vRand1, 0, % vRange

	vRange -= vRand1
	Random, vRand2, 0, % vRange

	vRange -= vRand2
	Random, vRand3, 0, % vRange

	vRand4 := vTotal - vRand3 - vRand2 - vRand1
	
	if !(vRand1>0 && vRand2>0 && vRand3>0 && vRand4>0)
		continue

	vOutput .= Format("{:0.2f}+{:0.2f}+{:0.2f}+{:0.2f}={:0.2f}", vRand1, vRand2, vRand3, vRand4, vTotal) "`r`n"	
	count ++
	if count = 10
		break
}

MsgBox % vOutput
after removed the +-3000, the test results will be like this
43573.08+1090.95+4961.54+2405.47=52031.04
308.71+37107.70+14552.70+61.93=52031.04
21725.26+844.35+18395.60+11065.84=52031.04
23153.71+623.68+26114.93+2138.72=52031.04
30166.40+2423.50+11438.81+8002.34=52031.04
17854.70+22524.75+4325.02+7326.57=52031.04
31560.52+11594.91+283.03+8592.58=52031.04
17613.05+11484.16+180.05+22753.78=52031.04
13606.52+33030.90+3373.58+2020.04=52031.04
17295.55+30087.31+1476.98+3171.20=52031.04

and yes, i actually need the vrand1 vrand2 vrand3 vrand4's value close to each other, thanks anyway for your help
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: Generate Random numbers and sum to specific given number

01 Sep 2017, 10:46

AlphaBravo wrote:
jeeswg wrote:vRange1 := (vTotal / 4) - 3000
vRange2 := (vTotal / 4) + 3000
why limit a "random" number to one quarter of vTotal +/- 3000 ??

Code: Select all

vTotal := 52031.04
Loop
{
	vRange := vTotal
	Random, vRand1, 0, % vRange

	vRange -= vRand1
	Random, vRand2, 0, % vRange

	vRange -= vRand2
	Random, vRand3, 0, % vRange

	vRand4 := vTotal - vRand3 - vRand2 - vRand1
	
	if !(vRand1>0 && vRand2>0 && vRand3>0 && vRand4>0)
		continue

	vOutput .= Format("{:0.2f}+{:0.2f}+{:0.2f}+{:0.2f}={:0.2f}", vRand1, vRand2, vRand3, vRand4, vTotal) "`r`n"	
	count ++
	if count = 10
		break
}

MsgBox % vOutput
according to jeeswg's version in the above,
but, is it possible to do a multiply to get the vRand1 2 3 4?
vRand1 := (Random number 3.40~4.59) * (Random number)
vRand2 := ;as vRand1 doing random stuff
vRand3 := ;as vRand1 doing random stuff
vRand4 := ;as vRand1 doing random stuff

i'm not sure did i make myself clear, sorry for my english
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Generate Random numbers and sum to specific given number  Topic is solved

01 Sep 2017, 15:49

So taking this example:
3264*4.52=14753.28
3264*4.07=13284.48
3264*3.84=12533.76
3312*3.46=11459.52

Each calculation is of the form:
#### * #.##
where the first number is (3200 <= #### <= 3400) (201 possibilities)
and the second number is (3.40 <= #.## <= 4.59) (120 possibilities) (you have stated both 4.50 and 4.59 as limits, and note: 4.52 is used above)

The thing is that in effect we are trying to find 8 integers such that:
a*b + c*d + e*f + g*h = 5203104
The fact that the numbers must be integers (or in this case hundredths) makes it very difficult to find numbers at random. I don't know the exact mathematics, potentially for a given number there are no solutions or very few solutions, and you would have to calculate them all by brute force combinatorics, and then pick a solution at random. The whole question seems quite advanced. Although there could be some mathematical trick that I am missing.

I would suggest that you give a bit more detail of the problem, in order for me or anyone else to be able to help you.

This script almost does what you want, except that one of the numbers is written to more than 2 decimal places:

Code: Select all

q:: ;split a number into 4 'random' parts
vTotal := 52031.04
vRangeN1 := 3200, vRangeN2 := 3400
vRangeM1 := 340, vRangeM2 := 450
vCount := 0
Loop
{
	if (vCount = 10)
		break
	Loop, 4
	{
		Random, vNum, % vRangeN1, % vRangeN2
		Random, vMult, % vRangeM1, % vRangeM2
		vNum%A_Index% := vNum*(vMult/100)
		vCalc%A_Index% := vNum "*" Format("{:0.2f}", vMult/100)
	}
	vNum4 := vTotal-vNum1-vNum2-vNum3
	if (vNum4 < vRangeN1*(vRangeM1/100))
	|| (vNum4 > vRangeN2*(vRangeM2/100))
		continue
	Loop
	{
		Random, vMult, % vRangeM1, % vRangeM2
		vNum := vNum4/(vMult/100)
		if (vNum < vRangeN1) || (vNum > vRangeN2)
			continue
		vCalc4 := vNum "*" Format("{:0.2f}", vMult/100)
			break
	}
	vCount++
	vOutput .= Format("{:0.2f}+{:0.2f}+{:0.2f}+{:0.2f}={:0.2f}", vNum1, vNum2, vNum3, vNum4, vTotal) "`r`n"
	vOutput2 .= Format("{:s}+{:s}+{:s}+{:s}={:0.2f}", vCalc1, vCalc2, vCalc3, vCalc4, vTotal) "`r`n"
}
Clipboard := vOutput "`r`n" vOutput2
MsgBox, % "done"
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Generate Random numbers and sum to specific given number

01 Sep 2017, 16:36

I believe that this code would work *in theory*, to list all the possible sums that would work, but there are so many numbers to test that it would take forever:

Code: Select all

q::
vRangeN1 := 3200, vRangeN2 := 3400
vRangeM1 := 340, vRangeM2 := 459
vTotal := 5203104

;(201*120)**4=338461452495360000
o1 := [vRangeN1,vRangeM1,vRangeN1,vRangeM1,vRangeN1,vRangeM1,vRangeN1,vRangeM1]
o2 := [vRangeN2,vRangeM2,vRangeN2,vRangeM2,vRangeN2,vRangeM2,vRangeN2,vRangeM2]

;a correct answer:
;3264*452 + 3264*407 + 3264*384 + 3312*346
;o1 := [3264,452,3264,407,3264,384,3312,346]
;o2 := [3264,452,3264,407,3264,384,3312,346]

o := o1.Clone(), o.0 := 0
vOutput := ""
Loop
{
	_vIndex := A_Index ;diagnostic, will appear at the top of the list of variables
	vSum := o.1*o.2+o.3*o.4+o.5*o.6+o.7*o.8
	;if (vSum = vTotal)
	;	MsgBox, % Format("{:i}*{:i}+{:i}*{:i}+{:i}*{:i}+{:i}*{:i}", o*) "=" vSum
	;ToolTip, % Format("{:i}*{:i}+{:i}*{:i}+{:i}*{:i}+{:i}*{:i}", o*) "=" vSum
	if (vSum = vTotal)
		vOutput .= Format("{:i}*{:i}+{:i}*{:i}+{:i}*{:i}+{:i}*{:i}", o*) "=" vSum "`r`n"
	o.8 += 1
	Loop, 8
	{
		vIndex := 9-A_Index
		if (o[vIndex] > o2[vIndex])
			o[vIndex] := o1[vIndex], o[vIndex-1] += 1
	}
	if (o.0)
		break
}
MsgBox, % vOutput
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: Generate Random numbers and sum to specific given number

01 Sep 2017, 20:30

jeeswg wrote:So taking this example:
3264*4.52=14753.28
3264*4.07=13284.48
3264*3.84=12533.76
3312*3.46=11459.52

Each calculation is of the form:
#### * #.##
where the first number is (3200 <= #### <= 3400) (201 possibilities)
and the second number is (3.40 <= #.## <= 4.59) (120 possibilities) (you have stated both 4.50 and 4.59 as limits, and note: 4.52 is used above)

The thing is that in effect we are trying to find 8 integers such that:
a*b + c*d + e*f + g*h = 5203104
The fact that the numbers must be integers (or in this case hundredths) makes it very difficult to find numbers at random. I don't know the exact mathematics, potentially for a given number there are no solutions or very few solutions, and you would have to calculate them all by brute force combinatorics, and then pick a solution at random. The whole question seems quite advanced. Although there could be some mathematical trick that I am missing.

I would suggest that you give a bit more detail of the problem, in order for me or anyone else to be able to help you.

This script almost does what you want, except that one of the numbers is written to more than 2 decimal places:

Code: Select all

q:: ;split a number into 4 'random' parts
vTotal := 52031.04
vRangeN1 := 3200, vRangeN2 := 3400
vRangeM1 := 340, vRangeM2 := 450
vCount := 0
Loop
{
	if (vCount = 10)
		break
	Loop, 4
	{
		Random, vNum, % vRangeN1, % vRangeN2
		Random, vMult, % vRangeM1, % vRangeM2
		vNum%A_Index% := vNum*(vMult/100)
		vCalc%A_Index% := vNum "*" Format("{:0.2f}", vMult/100)
	}
	vNum4 := vTotal-vNum1-vNum2-vNum3
	if (vNum4 < vRangeN1*(vRangeM1/100))
	|| (vNum4 > vRangeN2*(vRangeM2/100))
		continue
	Loop
	{
		Random, vMult, % vRangeM1, % vRangeM2
		vNum := vNum4/(vMult/100)
		if (vNum < vRangeN1) || (vNum > vRangeN2)
			continue
		vCalc4 := vNum "*" Format("{:0.2f}", vMult/100)
			break
	}
	vCount++
	vOutput .= Format("{:0.2f}+{:0.2f}+{:0.2f}+{:0.2f}={:0.2f}", vNum1, vNum2, vNum3, vNum4, vTotal) "`r`n"
	vOutput2 .= Format("{:s}+{:s}+{:s}+{:s}={:0.2f}", vCalc1, vCalc2, vCalc3, vCalc4, vTotal) "`r`n"
}
Clipboard := vOutput "`r`n" vOutput2
MsgBox, % "done"
return
This almost done, except the vNum4 is written to 6 decimal places
and i think we can simply round it down, add something like:
SetFormat Float, 0.2
???

another issue is the vCount++, i do not need it, just each time after i press Q button, if (vCount = 1)
break

thank you, could you tweak it a little bit?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Generate Random numbers and sum to specific given number

01 Sep 2017, 21:01

Here are some sums with the 6dp number:
3319*4.02+3221*3.58+3212*3.74+3395.650224*4.46 = 52031.039999
3266*3.52+3234*4.03+3278*4.12+3270.172897*4.28 = 52031.039999
3374*3.97+3209*3.66+3277*3.68+3295.991111*4.50 = 52031.04
3324*3.72+3270*4.24+3229*4.45+3266.260000*3.50 = 52031.04
3215*3.46+3214*3.70+3360*4.28+3333.608200*4.39 = 52031.039998
3363*3.48+3270*4.22+3242*3.78+3350.619718*4.26 = 52031.039999
3299*4.33+3220*3.84+3329*4.06+3250.912329*3.65 = 52031.040001
3336*4.05+3244*4.42+3367*3.81+3281.355491*3.46 = 52031.039999
3292*3.97+3350*4.21+3337*3.52+3229.571429*4.06 = 52031.040002
3362*4.18+3280*3.74+3217*3.71+3368.119804*4.09 = 52031.039998

If you round those numbers to the nearest integer, the sum is not *exact*:
3319*4.02+3221*3.58+3212*3.74+3396*4.46 = 52032.6
3266*3.52+3234*4.03+3278*4.12+3270*4.28 = 52030.3
3374*3.97+3209*3.66+3277*3.68+3296*4.50 = 52031.08
3324*3.72+3270*4.24+3229*4.45+3266*3.50 = 52030.13
3215*3.46+3214*3.70+3360*4.28+3334*4.39 = 52032.76
3363*3.48+3270*4.22+3242*3.78+3351*4.26 = 52032.66
3299*4.33+3220*3.84+3329*4.06+3251*3.65 = 52031.36
3336*4.05+3244*4.42+3367*3.81+3281*3.46 = 52029.81
3292*3.97+3350*4.21+3337*3.52+3230*4.06 = 52032.78
3362*4.18+3280*3.74+3217*3.71+3368*4.09 = 52030.55

To get an exact sum is a complicated problem mathematically, as I described earlier.

To give an extreme example, it's a bit like trying to solve Fermat's Last Theorem. Unless there are some key optimisations or mathematical principles I'm unaware of.

This is a very specific problem, it seems pure mathematical rather than practical.

I can't think of a way to give you a better script than what I've given you already.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: Generate Random numbers and sum to specific given number

02 Sep 2017, 02:33

jeeswg wrote:Here are some sums with the 6dp number:
3319*4.02+3221*3.58+3212*3.74+3395.650224*4.46 = 52031.039999
3266*3.52+3234*4.03+3278*4.12+3270.172897*4.28 = 52031.039999
3374*3.97+3209*3.66+3277*3.68+3295.991111*4.50 = 52031.04
3324*3.72+3270*4.24+3229*4.45+3266.260000*3.50 = 52031.04
3215*3.46+3214*3.70+3360*4.28+3333.608200*4.39 = 52031.039998
3363*3.48+3270*4.22+3242*3.78+3350.619718*4.26 = 52031.039999
3299*4.33+3220*3.84+3329*4.06+3250.912329*3.65 = 52031.040001
3336*4.05+3244*4.42+3367*3.81+3281.355491*3.46 = 52031.039999
3292*3.97+3350*4.21+3337*3.52+3229.571429*4.06 = 52031.040002
3362*4.18+3280*3.74+3217*3.71+3368.119804*4.09 = 52031.039998

If you round those numbers to the nearest integer, the sum is not *exact*:
3319*4.02+3221*3.58+3212*3.74+3396*4.46 = 52032.6
3266*3.52+3234*4.03+3278*4.12+3270*4.28 = 52030.3
3374*3.97+3209*3.66+3277*3.68+3296*4.50 = 52031.08
3324*3.72+3270*4.24+3229*4.45+3266*3.50 = 52030.13
3215*3.46+3214*3.70+3360*4.28+3334*4.39 = 52032.76
3363*3.48+3270*4.22+3242*3.78+3351*4.26 = 52032.66
3299*4.33+3220*3.84+3329*4.06+3251*3.65 = 52031.36
3336*4.05+3244*4.42+3367*3.81+3281*3.46 = 52029.81
3292*3.97+3350*4.21+3337*3.52+3230*4.06 = 52032.78
3362*4.18+3280*3.74+3217*3.71+3368*4.09 = 52030.55

To get an exact sum is a complicated problem mathematically, as I described earlier.

To give an extreme example, it's a bit like trying to solve Fermat's Last Theorem. Unless there are some key optimisations or mathematical principles I'm unaware of.

This is a very specific problem, it seems pure mathematical rather than practical.

I can't think of a way to give you a better script than what I've given you already.
Hi, jeeswg, glad to learn a lot from you, thanks,
i found that if the value
vRangeN2 := 3400
replace with 3550
then it is possible to get exact result without 6 dp
3319*4.02+3221*3.58+3212*3.74+3522*4.3 = 52031.04

but somehow it's not working after the vRangeN2 edited
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: Generate Random numbers and sum to specific given number

04 Sep 2017, 21:20

jeeswg wrote:Here are some sums with the 6dp number:
3319*4.02+3221*3.58+3212*3.74+3395.650224*4.46 = 52031.039999
3266*3.52+3234*4.03+3278*4.12+3270.172897*4.28 = 52031.039999
3374*3.97+3209*3.66+3277*3.68+3295.991111*4.50 = 52031.04
3324*3.72+3270*4.24+3229*4.45+3266.260000*3.50 = 52031.04
3215*3.46+3214*3.70+3360*4.28+3333.608200*4.39 = 52031.039998
3363*3.48+3270*4.22+3242*3.78+3350.619718*4.26 = 52031.039999
3299*4.33+3220*3.84+3329*4.06+3250.912329*3.65 = 52031.040001
3336*4.05+3244*4.42+3367*3.81+3281.355491*3.46 = 52031.039999
3292*3.97+3350*4.21+3337*3.52+3229.571429*4.06 = 52031.040002
3362*4.18+3280*3.74+3217*3.71+3368.119804*4.09 = 52031.039998

If you round those numbers to the nearest integer, the sum is not *exact*:
3319*4.02+3221*3.58+3212*3.74+3396*4.46 = 52032.6
3266*3.52+3234*4.03+3278*4.12+3270*4.28 = 52030.3
3374*3.97+3209*3.66+3277*3.68+3296*4.50 = 52031.08
3324*3.72+3270*4.24+3229*4.45+3266*3.50 = 52030.13
3215*3.46+3214*3.70+3360*4.28+3334*4.39 = 52032.76
3363*3.48+3270*4.22+3242*3.78+3351*4.26 = 52032.66
3299*4.33+3220*3.84+3329*4.06+3251*3.65 = 52031.36
3336*4.05+3244*4.42+3367*3.81+3281*3.46 = 52029.81
3292*3.97+3350*4.21+3337*3.52+3230*4.06 = 52032.78
3362*4.18+3280*3.74+3217*3.71+3368*4.09 = 52030.55

To get an exact sum is a complicated problem mathematically, as I described earlier.

To give an extreme example, it's a bit like trying to solve Fermat's Last Theorem. Unless there are some key optimisations or mathematical principles I'm unaware of.

This is a very specific problem, it seems pure mathematical rather than practical.

I can't think of a way to give you a better script than what I've given you already.
Hi jeeswg,i have to admit the request from this thread is more like impossible as you said, so i decided to start another post that would be easier to achieve,
could you check it out : Split specified number into parts and sum
thanks for your help
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Generate Random numbers and sum to specific given number

04 Sep 2017, 22:32

True, it does look pretty difficult, but you did calculate some values, where did you get those values from?
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: peter_ahk and 346 guests