jdhedden has asked for the wisdom of the Perl Monks concerning the following question:

According to Michael Howard of Microsoft, you can obtain true random numbers under Windows XP without having to deal with their full-blown CryptAPI.

Using XS, I have written a function to return an array ref of random ints using the referenced API.

#include <windows.h> SV * random(count) int count CODE: /* REFERENCE: http://blogs.msdn.com/michael_howard/archive/2005/01/14/353379 +.aspx */ /* Open the library */ HMODULE lib = LoadLibrary("ADVAPI32.DLL"); if (! lib) { Perl_croak(aTHX_ "ADVAPI32.DLL not found"); } /* 'Extract' the function */ BOOLEAN (APIENTRY *RtlGenRandom)(void*, ULONG) = (BOOLEAN (APIENTRY *)(void*,ULONG))GetProcAddress(lib, "SystemFunct +ion036"); if (! RtlGenRandom) { FreeLibrary(lib); Perl_croak(aTHX_ "RtlGenRandom() not found in ADVAPI32.dll"); } /* Set up a temporary buffer */ U32 *buff = (U32 *)malloc(count * sizeof(U32)); /* Get the random data */ if (! RtlGenRandom(buff, (ULONG)(count * sizeof(U32)))) { free(buff); FreeLibrary(lib); Perl_croak(aTHX_ "RtlGenRandom() failed"); } /* Copy the data to a Perl array */ AV *array = newAV(); int ii; for (ii=0; ii<count; ii++) { av_push(array, newSVuv(buff[ii])); } /* Cleanup */ free(buff); FreeLibrary(lib); /* Return an array ref */ RETVAL = newRV((SV *)array); OUTPUT: RETVAL
I would like to have a Perl version of this that uses Win32::API. The problem is that I use Perl under Cygwin, and Win32::API won't compile under Cygwin. Would someone be so good as to assist in translating this for me? Thank you.

(Yes, I know that /dev/random is available under Cygwin. I want to provide the code to others that are using Perl on PCs, but are not using Cygwin.)


Remember: There's always one more bug.

Replies are listed 'Best First'.
Re: Random Numbers under XP: Translating XS to Win32::API
by BrowserUk (Patriarch) on Jun 29, 2005 at 19:34 UTC

    Are you having problems? :)

    There is no entrypoint 'RtlGenRandom' exported from advapi32.dll. The function documented under that name is exported as 'SystemFunction36' (and may go away in future versions).

    #! perl -slw use strict; sub random { my $count = shift; use Win32::API::Prototype; ApiLink( 'ADVAPI32.DLL', 'BOOLEAN SystemFunction036( PVOID b, ULON +G n )' ) or die $^E; our $RtlGenRandom; *RtlGenRandom = *SystemFunction036; my $buffer = chr( 0 ) x ( 4 * $count ); if( RtlGenRandom( $buffer, 4 * $count ) ) { return [ unpack 'V*', $buffer ]; } else { warn "rtlGenRandom failed : $^E"; return; } } print for @{ random( 10 ) }; __END__ P:\test>471107 Name "main::SystemFunction036" used only once: possible typo at P:\tes +t\471107.pl line 11. 2938396619 3012541328 434618806 2806772272 1411585649 1621144589 2620908003 1934412219 3486653462 3699174573

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      Super! That's just what I needed.

      I'll probably eliminate the use of the function 'renaming', and just call SystemFunction036 (with appropriate comments for clarity).

      I noted your use of the 'V*' directive for the unpack call. Although with random data, distinguishing between big-endian and little-endian is superfluous.

      Thanks for the code!


      Remember: There's always one more bug.
        I noted your use of the 'V*' directive for the unpack call. Although with random data, distinguishing between big-endian and little-endian is superfluous.

        True, but as the platform is little-endian, converting using V* rather than N* saves doing a lot of fiddly byte and bit-twiddling. This really shows up when you unpack a few thousand ulongs.

        Below I timed unpacking the same 1000 values both ways:

        P:\test>471107 Name "main::SystemFunction036" used only once: possible typo at P:\tes +t\471107.pl line 14. 1 trial of N* non-native BigEndian ( 1.848ms total), 1.848ms/trial 1 trial of V* native LittleEndian ( 310us total), 310us/trial

        It essentially is the difference between doing nothing and doing something (fiddly).


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.