Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Random Numbers under XP: Translating XS to Win32::API

by BrowserUk (Patriarch)
on Jun 29, 2005 at 19:34 UTC ( [id://471149]=note: print w/replies, xml ) Need Help??


in reply to Random Numbers under XP: Translating XS to Win32::API

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.

Replies are listed 'Best First'.
Re^2: Random Numbers under XP: Translating XS to Win32::API
by jdhedden (Deacon) on Jun 29, 2005 at 20:05 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://471149]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-24 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found