Fix Random type, tests
Mostly-failed Optimize function