Generating two vector of same sum

I assume you want another random vector, can you just re-scale it?

julia> a = rand(1000:2000,5)
5-element Vector{Int64}:
 1484
 1053
 1135
 1863
 1823

julia> b = rand(1000:2000,10)
10-element Vector{Int64}:
 1784
 1708
 1512
 1169
 1703
 1394
 1855
 1520
 1645
 1652

julia> b = b * sum(a)/sum(b)
10-element Vector{Float64}:
 823.4018316396939
 788.3241751348639
 697.8607452013549
 539.5497428177142
 786.0164345753356
 643.3980679964873
 856.1717475849956
 701.5531300966002
 759.2466440848074
 762.477480868147

julia> sum(b) ≈ sum(a)
true

julia> sum(b) == sum(a) #I don't think this is always true
true

If you need Integer, I suppose you can just trunc and wiggle a single element value until the sum matches.

2 Likes