Random number generation API in kernel

All,
Can any one let me know of any kernel API for generating random numbers in
kernel.
Thanks in advance,
karthik

“Karthikeyan M” wrote in message
news:xxxxx@ntdev…
>
> Can any one let me know of any kernel API for generating random numbers in
> kernel.
>

I can tell you that for sure there isn’t a documented/supported DDI for
this.

Peter
OSR

Actually, this is one time I have to disagree with Peter, there is a
documented API in the IFS kit. Try:

ULONG RtlRandom(IN OUT PULONG Seed);

The results will be a random number.

Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting

----- Original Message -----
From: “Peter Viscarola”
Newsgroups: ntdev
To: “Windows System Software Devs Interest List”
Sent: Thursday, September 11, 2003 8:31 AM
Subject: [ntdev] Re: Random number generation API in kernel

>
> “Karthikeyan M” wrote in message
> news:xxxxx@ntdev…
> >
> > Can any one let me know of any kernel API for generating random numbers
in
> > kernel.
> >
>
> I can tell you that for sure there isn’t a documented/supported DDI for
> this.
>
> Peter
> OSR
>
>
>
> —
> Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@acm.org
> To unsubscribe send a blank email to xxxxx@lists.osr.com

Something like:

LARGE_INTEGER p = KeQueryPerformanceCounter(NULL);
DWORD random = p.LowPart ^ (DWORD) p.HighPart);

may deliver some arbitrary Number ( depends on timing ).

Sometimes the result(‘random’) will exactly predict the value of the
EIP-register of your next BSOD :slight_smile:

Norbert.

“Keep your words soft & sweet; you never know when you might have to
eat them.”
---- snip ----

> > Can any one let me know of any kernel API for generating random numbers
in
> > kernel.
> >
---- snip ----

There may not be any kernal API, but it’s not exactly a hard thing to
implement. There are many implementations freely available (including the
CRT source that comes with VC++).

Speaking of which, while I don’t normally recommend linking the CRT or
using the CRTL functions in the kernel, I don’t see any harm in using
srand in particular (though you probably will need to seed it yourself
since time() is unlikely to work). It’s a pretty simple function when all
is said and done.

Also, depending on your needs, you could always use a user-mode component
to call the CryptoAPI randomization functions and pass them down to your
driver. In fact, it’s remotely possible that there’s some interface to one
of the CAPI provider drivers that might be usable for this, but I don’t
have time to track that down.

BTW, does any one knows what is the difference between RtlRandom and
RtlRandomEx (which was introduced in Windows XP)? Their protos are the same.

-htfv

----- Original Message -----
From: “Don Burn”
To: “Windows System Software Devs Interest List”
Sent: Thursday, September 11, 2003 3:35 PM
Subject: [ntdev] Re: Random number generation API in kernel

> Actually, this is one time I have to disagree with Peter, there is a
> documented API in the IFS kit. Try:
>
> ULONG RtlRandom(IN OUT PULONG Seed);
>
> The results will be a random number.
>
> Don Burn (MVP, Windows DDK)
> Windows 2k/XP/2k3 Filesystem and Driver Consulting
>
> ----- Original Message -----
> From: “Peter Viscarola”
> Newsgroups: ntdev
> To: “Windows System Software Devs Interest List”
> Sent: Thursday, September 11, 2003 8:31 AM
> Subject: [ntdev] Re: Random number generation API in kernel
>
>
> >
> > “Karthikeyan M” wrote in message
> > news:xxxxx@ntdev…
> > >
> > > Can any one let me know of any kernel API for generating random
numbers
> in
> > > kernel.
> > >
> >
> > I can tell you that for sure there isn’t a documented/supported DDI for
> > this.
> >
> > Peter
> > OSR
> >
> >
> >
> > —
> > Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as: xxxxx@acm.org
> > To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>
> —
> Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@vba.com.by
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>

“Alexey Logachyov” wrote in message news:xxxxx@ntdev…
>
> BTW, does any one knows what is the difference between RtlRandom and
> RtlRandomEx (which was introduced in Windows XP)? Their protos are the
same.
>

I bow to, and gratefully acknowledge, Don’s superior command of the RTL
functions. I could have SWORN that I went looking for this myself a while
back. Thanks Don!

According to the IFS Kit docs, The RtlRandomEx function is an improved
version of RtlRandom that is twice as fast and produces “better” random
numbers. One might assume that “better” in this context means more evenly
distributed?

Thanks again to Don for checking the IFS kit docs!

Peter
OSR

> According to the IFS Kit docs, The RtlRandomEx function is an improved

version of RtlRandom that is twice as fast and produces “better”
random
numbers. One might assume that “better” in this context means more
evenly
distributed?

“Better” probably means more statistically random and/or a longer
period. Linear-congruential type generators are notorious for their
statistically low randomness and other defects (like an oscillating
least significant bit). Knuth’s book has a very interesting discussion
on the topic of pseudo-random number generation. It’s a good read, even
for the casually curious.

Chuck

RtlRandom from the IFS kit.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

----- Original Message -----
From: “Karthikeyan M”
To: “Windows System Software Devs Interest List”
Sent: Thursday, September 11, 2003 11:16 AM
Subject: [ntdev] Random number generation API in kernel

> All,
> Can any one let me know of any kernel API for generating random numbers in
> kernel.
> Thanks in advance,
> karthik
>
> —
> Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@storagecraft.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com

“Peter Viscarola” wrote in message news:xxxxx@ntdev…
>
> According to the IFS Kit docs, The RtlRandomEx function is an improved
> version of RtlRandom that is twice as fast and produces “better” random
> numbers. One might assume that “better” in this context means more evenly
> distributed?

I remember looking into a problem some years ago where one possible cause
of the symptoms was low variation in a random number. It turned out that the
random number generator routine consisted of the line

return 347; /* looks pretty random to me */

It wasn’t hard to make it better.