Talk:Seed

From Esolang
Jump to navigation Jump to search

possible seed that produces seed? Dittoslash (talk) 19:37, 22 March 2016 (UTC)

Ruby version of the reference Seed converter

#!/usr/bin/env ruby

length, seed = ARGV[0..1]
seed = seed.to_i

# Adjust seeds to match Python3 reference seed.py results
if seed < 1<<32
  seed += 1<<32
elsif seed < 1<<33
  seed += 1<<64
end

srand(seed)

chars = (' ' .. '~').to_a << "\n"
prog = (1..length.to_i).map {|i| chars[(rand * 96).to_i]}

puts "Your program:\n----------------------------------------\n", prog*"", "----------------------------------------\n"

Hopefully this works the same as the reference interpreter for all possible seeds. If there are gaps, please comment to alert me. I was playing with Mersenne twister seeding and trying to get reproducible results across languages. It seems Python has custom (array based) seeding, so while other languages may use seeds the same way, the integer seeds for Seed cannot simply be dropped into other standard mt implementations.

For example:

Python3

 random.seed(0); random.random()
 => 0.8444218515250481

Ruby

 srand(0);rand
 => 0.5488135039273248 

C++

 mt19937 generator (0);
 cout << "Random value: " << generator() * pow(2, -32);
 => 0.548814

Which demonstrates C++ and Ruby give the same value with seed 0, but not Python. There is an older python seed method (init_genrand(), vs. init_by_array()) which does give the 0.5488135039273248 result. I still haven't worked out how to get 0.8444218515250481 from the C++ mt19937 with a single seed value. It might not be possible without editing the twister state directly? Salpynx (talk) 04:47, 15 December 2021 (UTC)