New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue with srand #172

Closed
keilmillerjr opened this Issue Jan 8, 2019 · 4 comments

Comments

Projects
None yet
2 participants
@keilmillerjr
Copy link

keilmillerjr commented Jan 8, 2019

I am using an app (attractmode mac os x) with squirrel lang 3.0.4. Having an issue with srand.

  • time() is generating unique times, verified printing to console.
  • srand(time()) is generating the same seed every time.

srand(0) generates a seed, but srand with anything else as a parameter creates a different but non unique seed. I can only obtain two different seeds. Any ideas? Here’s a link to my project rantro-intro.

@zeromus

This comment has been minimized.

Copy link
Contributor

zeromus commented Jan 8, 2019

works fine for me, using some 1 year old build of squirrel, and I don't see any reason why it wouldn't work. The problem is probably best addressed to whoever integrated squirrel in attractmode. It's possible it takes ownership of srand (using it for its own internal purposes) and either intends for you to use another rand which it provided, or forgot to provide you a new one. in general using functions like this tightly coupled to the CRT is a poor idea. IMO you shouldn't be using these in an embedded squirrel.

@keilmillerjr

This comment has been minimized.

Copy link

keilmillerjr commented Jan 10, 2019

Apparently seems need to be vastly different to provide proper results. Time in seconds is not enough. I looked at some python libraries for reference. Thank you.

@zeromus

This comment has been minimized.

Copy link
Contributor

zeromus commented Jan 10, 2019

time in seconds should be adequate to get different results from srand (any input should lead to quite different output), but watch out for the common mistake that time() does only return time in seconds so if you srand(time()) several times in quick succession it will have no useful randomizing.

also note that using the system provided rand isnt guaranteed to give consistent results and can actually give WEIRD and BROKEN results on some systems due to poor quality, depending on how you use it. Also watch out, finding a simple C RNG and porting it to squirrel (for deterministic results) might be a good idea, but it will be easy to make a mistake that will break it. I would suggest suggesting to the attractmode authors to integrate an official portable/deterministic RNG and exposing it to their squirrel bindings.

@keilmillerjr

This comment has been minimized.

Copy link

keilmillerjr commented Jan 10, 2019

@zeromus "quick succession it will have no useful randomizing" was the exact issue I have seen. The function I have now seems to be working great on my mac. I’m not too worried since about it breaking since its a simple method of bit shifting that I have used on PLC’s. What would be good values was unknown to me, so some one else seemed to know more than I. I will have a friend look help through the app source and maybe I can integrate and provide a pull request. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment