Old submission, changes at the bottom

Inspiration

There was a challenge to build a random number generator. I thought, why not go for as bare-bones as possible, and code the whole thing in assembly.

What it does

There are two functions in the assembly code, mysrand and myrand.

mysrand gets given a 64-bit long int, and seeds the random number generator with it. Please note that if the seed takes less than 32 bits, it is useless.

myrand generates pseudo-random numbers using a modified middle-square algorithm straight from wikipedia. It takes no parameters, and returns a 64-bit long integer.

If you want to use these in assembly, the parameter is passed through rdi, and return values are passed through rax.

How we built it

I hacked it together in about 15 minutes.

Challenges we ran into

For a while I didn't understand why my tiny seeds weren't changing anything. Then I realised that they were too small.

Also, for some reason I needed to pass -no-pie to my linker, which is sad. Pie is nice.

Accomplishments that we're proud of

Getting it to work with c programs. Remarkably easy, actually. more people should do it.

What we learned

How easily C integrates with assembly. None of that multi-object nonsense python does.

What's next for x86_64 assembly middle-square algorithm

middle-square is all well and good, but it actually isn't. It is a terrible pseudo-random number generator (although the modified version seems ok), and you should be ashamed of thinking that you might use this library. So, if I ever work on this again, I will implement a better algorithm, more similar to what c's rand() actually uses.

Changes done for INIT:

I revisited this project, and fixed my biggest problem with it: the problem with the seeds. Now, instead of simply adding a tiny seed that just gets drowned out in in a single iteration, the seed is artificially inflated in such a way as to introduce change for more steps. Please note that single-digit seeds are still swallowed in about eight or nine steps.

Built With

Share this project:

Updates

Private user

Private user posted an update —

copied from above: I revisited this project, and fixed my biggest problem with it: the problem with the seeds. Now, instead of simply adding a tiny seed that just gets drowned out in in a single iteration, the seed is artificially inflated in such a way as to introduce change for more steps. Please note that single-digit seeds are still swallowed in about eight or nine steps.

Log in or sign up for Devpost to join the conversation.

Submission history