Skip to content
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

Add deterministic rand: d_rand() #15

Merged
1 commit merged into from
Feb 8, 2017
Merged

Add deterministic rand: d_rand() #15

1 commit merged into from
Feb 8, 2017

Conversation

oboroc
Copy link
Contributor

@oboroc oboroc commented Feb 6, 2017

asMSX will generate same binaries across platforms and compilers for
programs that use random macro

asMSX will generate same binaries across platforms and compilers for
programs that use random macro
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that the seed variable scope is the global scope so each time the function is called, the value won't be the same, hence producing a pseudo-random sequence. Am I right?

@oboroc
Copy link
Contributor Author

oboroc commented Feb 8, 2017

Initial seed is stored in a global static variable. With built-in C library you can call srand(unsigned int) to set a new seed value. And rand() returns a value between I think 1 and 32767. I used rand() implementation from ANSI C standard:

static unsigned long int next = 1;

int rand() {
    next = next * 1103515245 + 12345;
    return (unsigned int)(next/65536) % 32768; /* RAND_MAX is 32767 */
}

void srand(unsigned int seed) {
    next = seed;
}

If you don't like ANSI C rand(), you can try adapting one of more fancy PRNGs, like Mersenne Twister.

I wanted a function that works just like rand(), but is platform and compiler independent. This way if you compile an assembly program that uses random marco, you'll get the same pseudo-random output in binary files. I didn't add srand(unsigned int) to dura.y, because asMSX never used runtime srand() to set a new PRNG seed.

Please accept this pull request, you're the only one who can do it right now. I am not sure if I can produce multiple pull requests against a repository, and I don't want to mix unrelated matters in the same pull request.

@ghost ghost merged commit f4fbfbb into Fubukimaru:master Feb 8, 2017
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant