# Generating two vector of same sum

**URL:** <https://discourse.julialang.org/t/generating-two-vector-of-same-sum/86147>\
**Category:** General Usage\
**Created:** [August 22, 2022, 5:13pm UTC](https://discourse.julialang.org/t/generating-two-vector-of-same-sum/86147 "2022-08-22T17:13:36Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [August 22, 2022, 5:13pm UTC](https://discourse.julialang.org/t/generating-two-vector-of-same-sum/86147/1 "2022-08-22T17:13:36Z")

</div>

Dear all,  
I need to generate two vectors (not the same length) whose sum is the same. I have the first one:

```julia
a = rand(1000:2000,5)
5-element Array{Int64,1}:
 1404
 1438
 1529
 1039
 1071

```

Supose that I want a another vector `b` with 10 entries whose sum is `sum(a)`, how can I do this in a simple way?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [August 22, 2022, 5:22pm UTC](https://discourse.julialang.org/t/generating-two-vector-of-same-sum/86147/2 "2022-08-22T17:22:56Z")

</div>

Trivially:

```julia
other = zeros(Int, 10)
other[1] = sum(a)

```

---

<div class="post-metadata">

**Author:** ![liamfdoherty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liamfdoherty/32/24516_2.png) [@liamfdoherty](https://discourse.julialang.org/u/liamfdoherty)\
**Post date:** [August 22, 2022, 5:22pm UTC](https://discourse.julialang.org/t/generating-two-vector-of-same-sum/86147/3 "2022-08-22T17:22:56Z")

</div>

It might be worth checking out the JuLie package, they have a `partitions` function that can get you all the partitions of an integer (optionally of a specified length):

[https://ulthiel.github.io/JuLie.jl/stable/combinatorics/#JuLie.partitions](https://ulthiel.github.io/JuLie.jl/stable/combinatorics/#JuLie.partitions)

---

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [August 22, 2022, 5:30pm UTC](https://discourse.julialang.org/t/generating-two-vector-of-same-sum/86147/4 "2022-08-22T17:30:27Z")

</div>

not. I want b with nonzero entries…

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [August 22, 2022, 5:32pm UTC](https://discourse.julialang.org/t/generating-two-vector-of-same-sum/86147/5 "2022-08-22T17:32:41Z")

</div>

You didn’t indicate additional constraints. Another option:

```julia
other = ones(Int, 10)
other[1] = sum(a)-(length(other)-1)

```

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [August 22, 2022, 5:32pm UTC](https://discourse.julialang.org/t/generating-two-vector-of-same-sum/86147/6 "2022-08-22T17:32:47Z")

</div>

Combinatorics.jl also has a `partitions` function. And the elements in the result are all positive i.e. no zero entries.

```julia
julia> partitions(10, 3) # you can iterate on this directly, and probably should
Combinatorics.FixedPartitions(10, 3)

julia> partitions(10, 3) |> collect 
8-element Vector{Vector{Int64}}:
 [8, 1, 1]
 [7, 2, 1]
 [6, 3, 1]
 [5, 4, 1]
 [6, 2, 2]
 [5, 3, 2]
 [4, 4, 2]
 [4, 3, 3]

```

_Edit_: For your purpose, you’re probably better off with one of the other options.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 22, 2022, 5:44pm UTC](https://discourse.julialang.org/t/generating-two-vector-of-same-sum/86147/7 "2022-08-22T17:44:34Z")

</div>

just to give an idea of a possible strategy.  
Obviously a lot depends on the real use you have to make of it.  
Having not further specified the constraints to the problem, many others could be relevant solutions.

```julia

s=sum(rand(1:100,20))
ps=[]
while s > 1
    p=rand(1:s)
    s-=p
    push!(ps,p)
end

```

**edit** another way is to generate a random array and add or subtract something to adjust the sum

---

<div class="post-metadata">

**Author:** ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)\
**Post date:** [August 22, 2022, 5:49pm UTC](https://discourse.julialang.org/t/generating-two-vector-of-same-sum/86147/8 "2022-08-22T17:49:25Z")

</div>

How about this one?

```julia
julia> a = rand(1000:2000, 5);

julia> b = rand(1000:2000, 10);

julia> c = [b; sum(a) - sum(b)];

julia> sum(c) == sum(a)
true

```

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [August 22, 2022, 6:09pm UTC](https://discourse.julialang.org/t/generating-two-vector-of-same-sum/86147/9 "2022-08-22T18:09:22Z")

</div>

The result there is length 11 (and can have a negative entry, and it’s not clear whether it’s assumed that the result should be all positive). Changing it a little bit:

```julia
julia> a = rand(1000:2000, 5);

julia> desiredlength = 10
10

julia> b = rand(1:sum(a)÷desiredlength, desiredlength - 1);

julia> c = [b; sum(a) - sum(b)]
10-element Vector{Int64}:
  209
  279
  188
  744
  627
  256
  643
  661
  624
 4081

julia> sum(c) == sum(a)
true

```

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [August 22, 2022, 6:25pm UTC](https://discourse.julialang.org/t/generating-two-vector-of-same-sum/86147/10 "2022-08-22T18:25:14Z")

</div>

@rocco_sprmnt21 's idea is also interesting, but requires a little tweaking for this specification.

```julia
julia> a = rand(1000:2000, 5);

julia> s = sum(a);

julia> b = similar(a, desiredlength);

julia> desiredlength = 10;

julia> for i in 1:desiredlength-1
         b[i] = rand(1:s-desiredlength)
         s -= b[i]
       end

julia> b[desiredlength] = s
12

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

julia> b
10-element Vector{Int64}:
 3984
 3021
  511
  730
  154
  102
   56
    6
   80
   12

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 22, 2022, 7:47pm UTC](https://discourse.julialang.org/t/generating-two-vector-of-same-sum/86147/11 "2022-08-22T19:47:18Z")

</div>

> [@rocco\_sprmnt21](#):
>
> `ps=[]`

If you ever see a bare `[]` in your code, beware! This is terrible for performance. What you want here is `Int[]`.

---

<div class="post-metadata">

**Author:** ![cchderrick](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cchderrick](https://discourse.julialang.org/u/cchderrick)\
**Post date:** [August 22, 2022, 8:51pm UTC](https://discourse.julialang.org/t/generating-two-vector-of-same-sum/86147/12 "2022-08-22T20:51:41Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [August 22, 2022, 9:01pm UTC](https://discourse.julialang.org/t/generating-two-vector-of-same-sum/86147/13 "2022-08-22T21:01:13Z")

</div>

Wonderfull! the best solution!!! Thanks a lot.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 23, 2022, 5:34am UTC](https://discourse.julialang.org/t/generating-two-vector-of-same-sum/86147/14 "2022-08-23T05:34:57Z")

</div>

```julia

na=10
nb=23
s=107

function part(s, n)
  v=[rand(1:rand(1:100), n);s]
  [diff(v);v[1]] 
end

julia> sum(part(s, na))==s
true

julia> sum(part(s, nb))==s
true

```

to get array of positive numbers

```julia
function snpart(s, n)
  a=[rand(1:rand(1:s-1), n);s]
  sort!(a)
  [diff(a);a[1]] 
end

```

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [August 23, 2022, 5:38pm UTC](https://discourse.julialang.org/t/generating-two-vector-of-same-sum/86147/15 "2022-08-23T17:38:08Z")

</div>

One more with geometrical arguments:

```julia
n=4
v = rand(n)
#x1+x2+x3+x4=s #all points (vectors )with sum s lie in this hyperplane
wn_1 = rand(n-1)
w= vcat(wn_1, [sum(v)-sum(wn_1)])
sum(v), sum(w)

```
