# How to seed the KMeans algorithm in Clustering.jl?

**URL:** <https://discourse.julialang.org/t/how-to-seed-the-kmeans-algorithm-in-clustering-jl/86288>\
**Category:** Data\
**Tags:** question, data, clustering\
**Created:** [August 24, 2022, 8:21pm UTC](https://discourse.julialang.org/t/how-to-seed-the-kmeans-algorithm-in-clustering-jl/86288 "2022-08-24T20:21:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [August 24, 2022, 8:21pm UTC](https://discourse.julialang.org/t/how-to-seed-the-kmeans-algorithm-in-clustering-jl/86288/1 "2022-08-24T20:21:57Z")

</div>

How do I make sure I get the same clusters everytime I run the following code:

```julia
using Clustering
using Random

# make a random dataset with 1000 random 5-dimensional points
X = rand(MersenneTwister(10),5, 1000)

# cluster X into 20 clusters using K-means
R = kmeans(X, 20; maxiter=200, display=:iter)

@assert nclusters(R) == 20 # verify the number of clusters

a = assignments(R) # get the assignments of points to clusters
c = counts(R) # get the cluster sizes
M = R.centers # get the cluster centers

```

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [August 24, 2022, 9:15pm UTC](https://discourse.julialang.org/t/how-to-seed-the-kmeans-algorithm-in-clustering-jl/86288/2 "2022-08-24T21:15:28Z")

</div>

In another package ([BetaML](https://github.com/sylvaticus/BetaML.jl)) you can pass a RNG object to the `kmeans` function, e.g. kmeans(x, 20,rng= ```  
MersenneTwister(10))`

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [August 24, 2022, 9:21pm UTC](https://discourse.julialang.org/t/how-to-seed-the-kmeans-algorithm-in-clustering-jl/86288/3 "2022-08-24T21:21:00Z")

</div>

Thanks, I will check out this package.

In Clustering.jl, I came across this [issue](https://github.com/JuliaStats/Clustering.jl/issues/214), but it doesn’t work for me. Setting `Random.seed!(1234)` still gave me different clusters every time.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [August 26, 2022, 6:58am UTC](https://discourse.julialang.org/t/how-to-seed-the-kmeans-algorithm-in-clustering-jl/86288/4 "2022-08-26T06:58:30Z")

</div>

Surprising. It works here. Just to confirm: you have to call `Random.seed!(1234)` again before each call to `rand` and `kmeans`.
