# Random numbers without seed

**URL:** <https://in-thread.sonic-pi.net/t/random-numbers-without-seed/8940>\
**Category:** Support, Help & Resources\
**Created:** [June 13, 2024, 9:20am UTC](https://in-thread.sonic-pi.net/t/random-numbers-without-seed/8940 "2024-06-13T09:20:11Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![H\_Tachibana](https://avatars.discourse-cdn.com/v4/letter/h/e68b1a/32.png) [@H\_Tachibana](https://in-thread.sonic-pi.net/u/H_Tachibana)\
**Post date:** [June 13, 2024, 9:20am UTC](https://in-thread.sonic-pi.net/t/random-numbers-without-seed/8940/1 "2024-06-13T09:20:11Z")

</div>

As a way to use random numbers without seed,

use\_random\_seed Time.new.usec  
or  
use\_random\_seed Random.new\_seed

but what is the best recommended method?

---

<div class="post-metadata">

**Author:** ![beryann](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/beryann/32/3133_2.png) [@beryann](https://in-thread.sonic-pi.net/u/beryann)\
**Post date:** [June 13, 2024, 10:04am UTC](https://in-thread.sonic-pi.net/t/random-numbers-without-seed/8940/2 "2024-06-13T10:04:52Z")

</div>

Hi @H_Tachibana did you also try?

```ruby
use_random_seed Time.now.to_i
live_loop :boom do
  play rrand(30, 110), amp: 0.4
  sleep 0.25
end

```

---

<div class="post-metadata">

**Author:** ![H\_Tachibana](https://avatars.discourse-cdn.com/v4/letter/h/e68b1a/32.png) [@H\_Tachibana](https://in-thread.sonic-pi.net/u/H_Tachibana)\
**Post date:** [June 13, 2024, 10:41am UTC](https://in-thread.sonic-pi.net/t/random-numbers-without-seed/8940/3 "2024-06-13T10:41:49Z")

</div>

Thanks.  
I did not know about Time.now.to\_i.

---

<div class="post-metadata">

**Author:** ![H\_Tachibana](https://avatars.discourse-cdn.com/v4/letter/h/e68b1a/32.png) [@H\_Tachibana](https://in-thread.sonic-pi.net/u/H_Tachibana)\
**Post date:** [June 13, 2024, 12:19pm UTC](https://in-thread.sonic-pi.net/t/random-numbers-without-seed/8940/4 "2024-06-13T12:19:39Z")

</div>

Time.now.to\_i is a 10-digit integer.  
Random.new\_seed is a 38-digit integer. (Seedless random number in Ruby)  
Time.new.usec is a 5-digit integer.

Would you all say that 38 digits is the best?

---

<div class="post-metadata">

**Author:** ![emlyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/emlyn/32/1460_2.png) [@emlyn](https://in-thread.sonic-pi.net/u/emlyn)\
**Post date:** [June 14, 2024, 3:28pm UTC](https://in-thread.sonic-pi.net/t/random-numbers-without-seed/8940/5 "2024-06-14T15:28:34Z")

</div>

If by “best” you mean most random, then `Random.new_seed` is the best option here.

I believe the way that random numbers work in Sonic Pi is that it has a predetermined list of “random” numbers, and the seed just chooses the starting point in that list. This is to ensure reproducibility, so that running the same code with the same seed will generate the same sounds even if it uses randomness.

One effect of this is that if you use a similar seed (e.g. increment it by 1), you will get the same sequence of numbers, just offset a bit (in this case by 1), like this:

```ruby
define :random_list do |seed|
  use_random_seed seed
  list = []
  10.times { list.push(rand_i(100)) }
  list
end

puts random_list(123)
puts random_list(123)
puts random_list(124)

# {run: 45, time: 0.0}
# ├─ [77, 8, 53, 70, 0, 19, 99, 24, 89, 46]
# ├─ [77, 8, 53, 70, 0, 19, 99, 24, 89, 46]
# └─ [8, 53, 70, 0, 19, 99, 24, 89, 46, 9]

```

If you use `Time.now.to_i`, it only changes once a second, so if not much time has passed since the last run, you will get a similar sequence.  
Similarly for `Time.new.usec`, this is the microseconds part of the time, which repeats every second, so you are likely to get a similar sequence when you rerun the code if it happens to be a similar fractional part of a second as the last time.  
In contrast, `Random.new_seed` returns a very large number that changes completely every time you call it, so you will start in a completely different part of the random sequence each time.

---

<div class="post-metadata">

**Author:** ![beryann](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/beryann/32/3133_2.png) [@beryann](https://in-thread.sonic-pi.net/u/beryann)\
**Post date:** [June 15, 2024, 8:47am UTC](https://in-thread.sonic-pi.net/t/random-numbers-without-seed/8940/6 "2024-06-15T08:47:09Z")

</div>

Thanks @emlyn for this interesting explanation! Do you mean that for `Random.new_seed`, each call to it, it will generate different numbers with no reproducibility possible? And how do you use it, like this f.i?:

```ruby
Random.new_seed

live_loop :seed do
play rrand(40, 90)
sleep 0.5
end

```

---

<div class="post-metadata">

**Author:** ![H\_Tachibana](https://avatars.discourse-cdn.com/v4/letter/h/e68b1a/32.png) [@H\_Tachibana](https://in-thread.sonic-pi.net/u/H_Tachibana)\
**Post date:** [June 15, 2024, 10:40am UTC](https://in-thread.sonic-pi.net/t/random-numbers-without-seed/8940/7 "2024-06-15T10:40:36Z")

</div>

SP program is

```ruby
use_random_seed Random.new_seed
live_loop :seed do
  play rrand(40, 90)
  sleep 0.5
end

```

But this programme keeps producing the same sound, doesn’t it?

---

<div class="post-metadata">

**Author:** ![emlyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/emlyn/32/1460_2.png) [@emlyn](https://in-thread.sonic-pi.net/u/emlyn)\
**Post date:** [June 15, 2024, 12:51pm UTC](https://in-thread.sonic-pi.net/t/random-numbers-without-seed/8940/8 "2024-06-15T12:51:08Z")

</div>

Oh, that’s very weird, I’m not sure what’s happening there, but it seems when the random seed is too big things break.  
You can get around this by restricting the random seed to a lower range:

```ruby
use_random_seed Random.new_seed % 1e9

```

---

<div class="post-metadata">

**Author:** ![emlyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/emlyn/32/1460_2.png) [@emlyn](https://in-thread.sonic-pi.net/u/emlyn)\
**Post date:** [June 15, 2024, 1:16pm UTC](https://in-thread.sonic-pi.net/t/random-numbers-without-seed/8940/9 "2024-06-15T13:16:27Z")

</div>

Hi @beryann, it’s almost like that, you just have to pass the new seed into Sonic Pi’s `use_random_seed`, but as @H_Tachibana found out it still doesn’t quite work because the numbers it generates are too big and Sonic Pi can’t handle them, so something like this should do the trick:

```ruby
use_random_seed Random.new_seed % 1e9

live_loop :seed do
    play rrand(40, 90)
    sleep 0.5
end

```

---

<div class="post-metadata">

**Author:** ![beryann](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/beryann/32/3133_2.png) [@beryann](https://in-thread.sonic-pi.net/u/beryann)\
**Post date:** [June 15, 2024, 2:02pm UTC](https://in-thread.sonic-pi.net/t/random-numbers-without-seed/8940/10 "2024-06-15T14:02:32Z")

</div>

ok Emelyn, thanks! @emlyn But then, are the sequences of notes predictable or not if I transmit my code to someone? Is there reproducibility? 🤔

---

<div class="post-metadata">

**Author:** ![emlyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/emlyn/32/1460_2.png) [@emlyn](https://in-thread.sonic-pi.net/u/emlyn)\
**Post date:** [June 15, 2024, 4:58pm UTC](https://in-thread.sonic-pi.net/t/random-numbers-without-seed/8940/11 "2024-06-15T16:58:50Z")

</div>

If you use `Random.new_seed` it will give you a new sequence each time.  
If you want it to be random-sounding but reproducible, you can just not call `use_random_seed` at all, or give it a fixed number. E.g. you could do something like:

```ruby
use_random_seed 123456

live_loop :seed do
    play rrand(40, 90)
    sleep 0.5
end

```

And change the random seed until you get a “random” sequence that you like. Then if you send the code to someone else they will get the same sequence.

---

<div class="post-metadata">

**Author:** ![beryann](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/beryann/32/3133_2.png) [@beryann](https://in-thread.sonic-pi.net/u/beryann)\
**Post date:** [June 16, 2024, 6:25am UTC](https://in-thread.sonic-pi.net/t/random-numbers-without-seed/8940/12 "2024-06-16T06:25:43Z")

</div>

Thanks a lot Emlyn @emlyn so, random is a question of choice in Sonic Pi, if I can say so! 😁  
Supplementary question: Doing this sounds logical but is it redundant compared to “use\_random\_seed 123456” ?

```ruby
use_random_seed rrand(1, 123456)

live_loop :seed do
    play rrand(40, 90)
    sleep 0.5
end

```

---

<div class="post-metadata">

**Author:** ![brendanmac](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/brendanmac/32/2809_2.png) [@brendanmac](https://in-thread.sonic-pi.net/u/brendanmac)\
**Post date:** [June 16, 2024, 9:12pm UTC](https://in-thread.sonic-pi.net/t/random-numbers-without-seed/8940/13 "2024-06-16T21:12:57Z")

</div>

Hi  
your code produces one random seed upon execution

```ruby
myrand = rrand(1, 123456)

use_random_seed myrand

live_loop :seed do
  play rrand(40, 90)
  sleep 0.5
  puts myrand
end

```

This helped me understand use\_random\_seed

[![](https://img.youtube.com/vi/Ise47Z5T8HQ/maxresdefault.jpg "Sonic Pi Tutorial - Pseudo-Random vs True Random") ](https://www.youtube.com/watch?v=Ise47Z5T8HQ)

Random.new\_seed is entirely random every time, anything else is pseudo-random, i.e. random repeatedly. Built in to Sonic Pi for reproducability.

---

<div class="post-metadata">

**Author:** ![emlyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/emlyn/32/1460_2.png) [@emlyn](https://in-thread.sonic-pi.net/u/emlyn)\
**Post date:** [June 19, 2024, 10:52am UTC](https://in-thread.sonic-pi.net/t/random-numbers-without-seed/8940/14 "2024-06-19T10:52:46Z")

</div>

> [@beryann](#):
>
> `use_random_seed rrand(1, 123456)`

That’s right, the above (even if it initially looks more random) is redundant. When `rrand` is called at the start of the run, it will always generate the same number, so you might as well just type a ‘random’ number yourself, no need to call `rrand`.

---

<div class="post-metadata">

**Author:** ![cubicinfinity](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/cubicinfinity/32/2355_2.png) [@cubicinfinity](https://in-thread.sonic-pi.net/u/cubicinfinity)\
**Post date:** [June 20, 2024, 2:46am UTC](https://in-thread.sonic-pi.net/t/random-numbers-without-seed/8940/15 "2024-06-20T02:46:27Z")

</div>

I’ve been needing this.

---

<div class="post-metadata">

**Author:** ![beryann](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/beryann/32/3133_2.png) [@beryann](https://in-thread.sonic-pi.net/u/beryann)\
**Post date:** [June 20, 2024, 12:31pm UTC](https://in-thread.sonic-pi.net/t/random-numbers-without-seed/8940/16 "2024-06-20T12:31:13Z")

</div>

You are are right Emelyn! @emlyn I forgot that my rrand generates the same number…These concepts are subtle and we have to pay attention to each of these terms! 🧐
