Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

cross platform solution for rand at CT (avoidining 'dummy seed' of windows and other cross platform issues) #12

Closed
timotheecour opened this issue Dec 3, 2018 · 2 comments

Comments

@timotheecour
Copy link

timotheecour commented Dec 3, 2018

/cc @ba0f3

potential cross platform solution for rand at CT (avoidining 'dummy seed' of windows and other cross platform issues):

  • make nimble install tempfile installs a binary (these go automatically in ~/.nimble/bin IIRC, on posix), say, tempfile_helper
    this binary returns the seed random.randomize() (which we can improve later, independently), or a custom proc, and prints its value
  • call it at CT using gorgeEx (let's say we can assume it's in PATH, or maybe allow passing a string in API to specify path to bin containing it)

note

  • this would fix date +'%N' doesn't work on OSX #11
  • this would fix windows case at CT (currently not really working because of dummy seed)
  • this would be more robust than relying on date +'%N' , and allow making it further more random by allowing to use pid and threadid, all in a clean way using pure nim code
@ba0f3
Copy link
Collaborator

ba0f3 commented Dec 3, 2018

too bad, getpid proc and times module does not work on compile time.

@ba0f3
Copy link
Collaborator

ba0f3 commented Dec 4, 2018

problem will be resolved once this PR get merged nim-lang/Nim#9851, refs nim-lang/Nim#8588

here is an example for getting random seed at compile time

import random

proc seeder() = discard
proc getSeed(p: pointer): int =
  cast[int](p)

proc test(): int {.compileTime.} =
  var
    seed = getSeed(seeder)
    r = initRand(seed)

  for i in 1..10:
    echo r.rand(int.high)

discard test()

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants