# Want to generate samples with all items being sampled the same number of times?

**URL:** <https://discourse.julialang.org/t/want-to-generate-samples-with-all-items-being-sampled-the-same-number-of-times/57351>\
**Category:** New to Julia\
**Created:** [March 17, 2021, 12:18am UTC](https://discourse.julialang.org/t/want-to-generate-samples-with-all-items-being-sampled-the-same-number-of-times/57351 "2021-03-17T00:18:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DevJac](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@DevJac](https://discourse.julialang.org/u/DevJac)\
**Post date:** [March 17, 2021, 12:18am UTC](https://discourse.julialang.org/t/want-to-generate-samples-with-all-items-being-sampled-the-same-number-of-times/57351/1 "2021-03-17T00:18:13Z")

</div>

Is there any library that can help me generate random “batches” of data such that every item appears in the same number of batches (with the exception that some items may appear one more time than other items)?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 17, 2021, 1:33am UTC](https://discourse.julialang.org/t/want-to-generate-samples-with-all-items-being-sampled-the-same-number-of-times/57351/2 "2021-03-17T01:33:20Z")

</div>

is it the same as:

```julia
julia> a = [1,2,3]
3-element Vector{Int64}:
 1
 2
 3

julia> bag = repeat(a, 2) #every item appears twice

julia> shuffle!(bag)

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 18, 2021, 2:58pm UTC](https://discourse.julialang.org/t/want-to-generate-samples-with-all-items-being-sampled-the-same-number-of-times/57351/3 "2021-03-18T14:58:42Z")

</div>

I am not sure I understand the specs, but I don’t think there is a specialized function for this (especially given the exception).

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [March 18, 2021, 3:38pm UTC](https://discourse.julialang.org/t/want-to-generate-samples-with-all-items-being-sampled-the-same-number-of-times/57351/4 "2021-03-18T15:38:49Z")

</div>

If you can post a small example of what the output should look like I’m certain you’ll get a good answer to this.

---

<div class="post-metadata">

**Author:** ![DevJac](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@DevJac](https://discourse.julialang.org/u/DevJac)\
**Post date:** [March 20, 2021, 4:05am UTC](https://discourse.julialang.org/t/want-to-generate-samples-with-all-items-being-sampled-the-same-number-of-times/57351/5 "2021-03-20T04:05:24Z")

</div>

Wrote my own:

```julia
struct Batcher{T}
    data :: Vector{T}
    batchsize :: Int
    Batcher(data; batchsize) = new{eltype(data)}(data, batchsize)
end

function Base.iterate(b::Batcher)
    shuffle!(b.data)
    iterate(b, 1)
end

function Base.iterate(b::Batcher, state)
    if b.batchsize > length(b.data)
        return (shuffle!(b.data), 1)
    end
    if state + b.batchsize-1 > length(b.data)
        post_shuffle_size = length(b.data) - state + 1
        result = b.data[state:end]
        shuffle!(b.data)
        (append!(result, b.data[1:post_shuffle_size]), post_shuffle_size+1)
    else
        (b.data[state:state+b.batchsize-1], (state + b.batchsize) % length(b.data))
    end
end

```
