Saved for later reference

online repository of stuff I had to google for hours to figure out – and random snippets of code

deadbeef random number generator in F#

Tags: , ,

I found the Deadbeef Random Number Generator when reading the Reddit thread “What’s the best / most interesting piece of code you’ve ever seen?” – and since I’m trying to learn F#, I figured I could try implementing it, first in an “imperative” way, then in a more functional way.

First the imperative way, a kind of naïve translation of the C code presented:

1
2
3
4
5
6
7
8
9
10
11
12
13
let mutable (deadbeef_seed:uint32) = uint32 0;
let mutable (deadbeef_beef:uint32) = uint32 0xdeadbeef;

let deadbeef_srand x =
    let oldseed = deadbeef_seed
    deadbeef_seed <- x
    deadbeef_beef <- uint32 0xdeadbeef
    oldseed

let deadbeef_rand () =
    deadbeef_seed <- (deadbeef_seed <<< 7) ^^^ ((deadbeef_seed >>> 25) + deadbeef_beef);
    deadbeef_beef <- (deadbeef_beef <<< 7) ^^^ ((deadbeef_beef >>> 25) + (uint32 0xdeadbeef));
    deadbeef_seed;

This is not very functional, with mutables and all that, so I tried again, with an implementation requiring no mutable variables:

1
2
3
4
5
6
7
8
9
let deadbeef_rand_seq seed = seq {
    let rec db_rand seed beef = seq {
        let newseed = (seed <<< 7) ^^^ ((seed >>> 25) + beef);
        let newbeef = (beef <<< 7) ^^^ ((beef >>> 25) + (uint32 0xdeadbeef));
        yield newseed
        yield! db_rand newseed newbeef
        }
    yield! db_rand seed (uint32 0xdeadbeef)
    }

…much better (at least in my opinion); implemented as a sequence of random numbers, deadbeef_rand_seq(seed).Take(10) would give you 10 random uint32s.
I’ll bet there are about 1000 other ways to make this more F#-like/more functional, so I’d love to hear about them!

Share

Simple Python ctypes Example – in Windows

Tags: , , , , , ,

I found this blog post, Simple Python ctypes Example, on Reddit, and thought I’d see how different the procedure was for Windows.

What you’ll need:

Creating the DLL

Create a file dlltest.c with the following content:

1
2
3
4
5
6
7
8
9
10
11
12
#include <windows.h>

BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
{
return TRUE;
}

__declspec(dllexport) int
multiply(int num1, int num2)
{
return num1 * num2;
}

Start the Visual Studio 2008 Command Prompt from the Start Menu (this sets up the path for the compiler and other files, which makes things easier) and go to the directory you saved dlltest.c to.

Compile it with the following command:

cl -LD dlltest.c -Fetest.dll

Testing the DLL

As in the original Linux example, this DLL is easily tested using similar commands:

1
2
3
4
>>> from ctypes import *
>>> libtest = cdll.LoadLibrary('test.dll')
>>> print libtest.multiply(2, 2)
4

So, that is the simplest DLL possible, I’ll look at (same as the original blog post) applying it to more complex examples later.

Share

Patched libgmail for unicode handling

Tags: , , , , ,

libgmail 0.1.11 has a bug with unicode handling, when the sender of an email has a name with one or more non-ASCII character in it.

I’ve applied the patch I described in the link above, adding the function:

1
2
3
4
5
6
7
8
9
10
def to_unicode(xstr):
    '''
    Forces string to unicode
    '''

    if type(xstr) == unicode:
        return xstr
    elif type(xstr) == str:
        return xstr.decode('utf-8')
    else:
        return xstr

And changed all calls to var.decode(‘utf-8′) to to_unicode(var).

libgmail-0.1.11p1.zip (source package)
libgmail-0.1.11p1.win32.exe (win32 installer)

Share

© 2009 Saved for later reference. All Rights Reserved.

This blog is powered by Wordpress and Magatheme by Bryan Helmig.