How generate a random string correctly?

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

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:

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:

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

In the command line, i get this:

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.

1 Like

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?)

1 Like

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

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.

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.

1 Like

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

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.

2 Likes

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