# How generate a random string correctly?

**URL:** https://discourse.julialang.org/t/how-generate-a-random-string-correctly/34241
**Category:** New to Julia
**Created:** [February 6, 2020, 8:32am UTC](https://discourse.julialang.org/t/how-generate-a-random-string-correctly/34241 "2020-02-06T08:32:17Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Darione](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/darione/32/12160_2.png) [@Darione](https://discourse.julialang.org/u/Darione)
#### Post date: [February 6, 2020, 8:32am UTC](https://discourse.julialang.org/t/how-generate-a-random-string-correctly/34241/1 "2020-02-06T08:32:17Z")

</div>

Hello,  
i was trying to generate a random string.  
Looking for something similar on google, i find this one way:

```julia
Random.seed!( 0 )
prefix_result = randstring(12)
println(prefix_result)

```

I understand that force seed to 0, Ialways get the same string, so I test in this way:

```julia
using Random
println("--------------------------")
#Random.seed!( rand(1:100000) )
prefix_result = randstring(12)
println(prefix_result)

Random.seed!( 0 )
prefix_result = randstring(12)
println(prefix_result)

Random.seed!( rand(1:100000) )
prefix_result = randstring(12)
println(prefix_result)

```

And i obtein differe behavoiur in June and running as script from command line:

In june i get the same string, if i run again and again my scrip:

```julia
--------------------------
6J6tq8qFzZvo
0IPrGg0JVONT
Hw3AOmXRzOiv
--------------------------
6J6tq8qFzZvo
0IPrGg0JVONT
Hw3AOmXRzOiv
--------------------------
6J6tq8qFzZvo
0IPrGg0JVONT
Hw3AOmXRzOiv
julia> 

```

In the command line, i get this:

```julia
c:\vmswap\juliaPrj\querere>julia rand.jl
--------------------------
BKKKCPvwjG86
0IPrGg0JVONT
Hw3AOmXRzOiv

c:\vmswap\juliaPrj\querere>julia rand.jl
--------------------------
XKVf5RArKQMe
0IPrGg0JVONT
Hw3AOmXRzOiv

c:\vmswap\juliaPrj\querere>julia rand.jl
--------------------------
SDKUTD3HG8S3
0IPrGg0JVONT
Hw3AOmXRzOiv

c:\vmswap\juliaPrj\querere>

```

I understand that i don’t have to set se seed if i want a random number every time (in the command line) … this is clear, ok, but I really do not understand why running in the juno REPL i get the same strings every time … what i am doing wrong?

Many thanks for reply.

---

<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: [February 6, 2020, 8:36am UTC](https://discourse.julialang.org/t/how-generate-a-random-string-correctly/34241/2 "2020-02-06T08:36:24Z")

</div>

Is it the same version of Julia running in both cases? You can check with `VERSION`.

(Also, is this a problem of practical concern? Do you need your random strings to be reproducible, or are you just curious?)

---

<div class="post-metadata">

### Author: ![Darione](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/darione/32/12160_2.png) [@Darione](https://discourse.julialang.org/u/Darione)
#### Post date: [February 6, 2020, 8:50am UTC](https://discourse.julialang.org/t/how-generate-a-random-string-correctly/34241/3 "2020-02-06T08:50:20Z")

</div>

Version is the same in windows command line and in JUNE:

```julia
julia> VERSION
v"1.3.0"

julia>  

```

I am porting my contextual search engine in Julia … and i need to generate every time a unique string, to collect many output file i have all toghether (es: rt83JX2\_file1.txt, rt83JX2\_file2.txt, … , rt83JX2\_file100.txt ). But i have notice that, running in Juno, i always get the same random strings …

Ah, i found this: if i stop, in Juno, the julia engine and run again , the first string change. Ok, the others must not change, because of the seed, ok, i undestand, corrects.

So, i have to restart engine in juno… or perhaps, i have to pass to the seed something like the current time?

This could help to generate always different string from juno … without restarting the Julia engine.

---

<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: [February 6, 2020, 8:58am UTC](https://discourse.julialang.org/t/how-generate-a-random-string-correctly/34241/4 "2020-02-06T08:58:53Z")

</div>

I am still not sure I understand what you are trying to do, but if you need _random_ strings, just don’t set the seed at all.

---

<div class="post-metadata">

### Author: ![Darione](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/darione/32/12160_2.png) [@Darione](https://discourse.julialang.org/u/Darione)
#### Post date: [February 6, 2020, 9:04am UTC](https://discourse.julialang.org/t/how-generate-a-random-string-correctly/34241/5 "2020-02-06T09:04:16Z")

</div>

Ahh … i have undestood … because i am in “Python running module mentality” … instead, Juno do not reset workspace if i run again … so it is sufficient to not set the seed …  
Ok, thanks @Tamas_Papp

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [February 6, 2020, 1:04pm UTC](https://discourse.julialang.org/t/how-generate-a-random-string-correctly/34241/6 "2020-02-06T13:04:32Z")

</div>

```julia
Random.seed!( 0 )
prefix_result = randstring(12)
println(prefix_result)

Random.seed!( rand(1:100000) )
prefix_result = randstring(12)

```

Note that the `rand(1:100000)` also uses the seed so you will get the same number from that every time and then `seed!` with that (same) number.

---

<div class="post-metadata">

### Author: ![Darione](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/darione/32/12160_2.png) [@Darione](https://discourse.julialang.org/u/Darione)
#### Post date: [February 6, 2020, 1:19pm UTC](https://discourse.julialang.org/t/how-generate-a-random-string-correctly/34241/7 "2020-02-06T13:19:17Z")

</div>

Yes, you are right … it was a conceptual error by me, i saw it later…
