Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Any Random Number Generators implementations that don't use CRT

I'm looking for a quick implementation of a random number generator in C/++ that can generate a random number within a range, that doesn't use the CRT.

I plan to paste it into my code for the time being, and when I get round to it improve it / write my own one.

I found this : http://www.bedaux.net/mtrand/
But it doesn't generate within a range, correct me if I'm wrong but I'm in a rush.

Thanks
Have you tried one of the Mersenne Twister implementations?

http://www.math.sci.hiroshima-u.ac.jp/~m...index.html
http://www.cs.hmc.edu/~geoff/mtwist.html

Hope this helps.


Sorry I just reread your post.

With the implementation you listed it generates a float between 0 and 1. If you want it between a specific range I think you need to use the % operator and + operator like this:

Code
mtrand() % 100;

I believe this example generates a random number between 0 and 99.

Code
mtrand() % 100 + 1;

And I believe this creates a random number between 1 and 100.

Hope this helps.
(Jan 25, 2014 04:23 AM)FurfUksake Wrote:  Have you tried one of the Mersenne Twister implementations?

http://www.math.sci.hiroshima-u.ac.jp/~m...index.html
http://www.cs.hmc.edu/~geoff/mtwist.html

Hope this helps.

Hey thanks, but the files you recommended have dependencies to CRT, unfortunately.

I will just go with CryptAcquireContext -> CryptGenRandom, and format the output from there.


(Jan 25, 2014 04:23 AM)FurfUksake Wrote:  Have you tried one of the Mersenne Twister implementations?

http://www.math.sci.hiroshima-u.ac.jp/~m...index.html
http://www.cs.hmc.edu/~geoff/mtwist.html

Hope this helps.


Sorry I just reread your post.

With the implementation you listed it generates a float between 0 and 1. If you want it between a specific range I think you need to use the % operator and + operator like this:

Code
mtrand() % 100;

I believe this example generates a random number between 0 and 99.

Code
mtrand() % 100 + 1;

And I believe this creates a random number between 1 and 100.

Hope this helps.

Lol, I completely forgot about this, I have another implementation I should have done this for, thanks alot, can't believe my stupidity.
RtlRandomEx()                           
(Jan 25, 2014 05:55 AM)IRP_MJ_SCSI Wrote:  RtlRandomEx()                           

I was going to suggest this too. You'll have to dynamically link to this function, but I like that it generates a 64 bit integer instead of a 32 bit one. And to generate within a range, I don't see this as being a big deal, just use the % operator...
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
OP i thought you were a "C/++ FASM software engineer"?
(Jan 25, 2014 06:00 PM)popov Wrote:  OP i thought you were a "C/++ FASM software engineer"?

I am huehuehuehue, just wanted a spoonfeed 😝


(Jan 25, 2014 05:55 AM)IRP_MJ_SCSI Wrote:  RtlRandomEx()                           

Yah is good, thanks for recommending this.
Code
typedef ULONG(WINAPI *TypeRtlRandom)(PULONG Seed);
TypeRtlRandom RtlRandom;

LPWSTR GenerateRandomNumbers(int Length)
{
    static wchar_t RetString[MAX_LINE];

    ULONG Seed = GetTickCount();

    HMODULE hlib = GetModuleHandleW(L"ntdll.dll");
    RtlRandom = (TypeRtlRandom)GetProcAddress(hlib, "RtlRandom");

    for(int i = NULL; i < Length; i++)
    {
      _snwprintf(&RetString[i], sizeof(RetString) - 1, L"%d", RtlRandom(&Seed)%9);
    }

    return RetString;
}

LPWSTR GenerateRandomLetters(int Length)
{
    static wchar_t RetString[MAX_LINE];

    ULONG Seed = GetTickCount();

    HMODULE hlib = GetModuleHandleW(L"ntdll.dll");
    RtlRandom = (TypeRtlRandom)GetProcAddress(hlib, "RtlRandom");

    for(int i = NULL; i < Length; i++)
    {
      RetString[i] = RtlRandom(&Seed)%26 + 97;
    }

    return RetString;
}

(Jan 25, 2014 05:50 PM)0xDEAD10CC Wrote:  I was going to suggest this too. You'll have to dynamically link to this function, but I like that it generates a 64 bit integer instead of a 32 bit one. And to generate within a range, I don't see this as being a big deal, just use the % operator...

You don't "have" to dynamically link ntdll lol
(Jan 27, 2014 12:41 PM)0x1B Wrote:  
Code
typedef ULONG(WINAPI *TypeRtlRandom)(PULONG Seed);
TypeRtlRandom RtlRandom;

LPWSTR GenerateRandomNumbers(int Length)
{
    static wchar_t RetString[MAX_LINE];

    ULONG Seed = GetTickCount();

    HMODULE hlib = GetModuleHandleW(L"ntdll.dll");
    RtlRandom = (TypeRtlRandom)GetProcAddress(hlib, "RtlRandom");

    for(int i = NULL; i < Length; i++)
    {
      _snwprintf(&RetString[i], sizeof(RetString) - 1, L"%d", RtlRandom(&Seed)%9);
    }

    return RetString;
}

LPWSTR GenerateRandomLetters(int Length)
{
    static wchar_t RetString[MAX_LINE];

    ULONG Seed = GetTickCount();

    HMODULE hlib = GetModuleHandleW(L"ntdll.dll");
    RtlRandom = (TypeRtlRandom)GetProcAddress(hlib, "RtlRandom");

    for(int i = NULL; i < Length; i++)
    {
      RetString[i] = RtlRandom(&Seed)%26 + 97;
    }

    return RetString;
}

You don't "have" to dynamically link ntdll lol

I'd probably manually encode the string though, because _snwprintf uses CRT also.
(Jan 25, 2014 05:55 AM)IRP_MJ_SCSI Wrote:  RtlRandomEx()

It works... kind of. But its randomness is very bad. Test it with ENT or NIST, RtlRandomEx is really a lousy PRNG.

Here you have some choice.
0x1B Wrote:
Code
typedef ULONG(WINAPI *TypeRtlRandom)(PULONG Seed);
TypeRtlRandom RtlRandom;

LPWSTR GenerateRandomNumbers(int Length)
{
    static wchar_t RetString[MAX_LINE];

    ULONG Seed = GetTickCount();

    HMODULE hlib = GetModuleHandleW(L"ntdll.dll");
    RtlRandom = (TypeRtlRandom)GetProcAddress(hlib, "RtlRandom");

    for(int i = NULL; i < Length; i++)
    {
      _snwprintf(&RetString[i], sizeof(RetString) - 1, L"%d", RtlRandom(&Seed)%9);
    }

    return RetString;
}

LPWSTR GenerateRandomLetters(int Length)
{
    static wchar_t RetString[MAX_LINE];

    ULONG Seed = GetTickCount();

    HMODULE hlib = GetModuleHandleW(L"ntdll.dll");
    RtlRandom = (TypeRtlRandom)GetProcAddress(hlib, "RtlRandom");

    for(int i = NULL; i < Length; i++)
    {
      RetString[i] = RtlRandom(&Seed)%26 + 97;
    }

    return RetString;
}

You don't "have" to dynamically link ntdll lol

You would suggest statically linking ntdll for a regular program? ❓ There's an underlying reason why I had those choice of words. And WHY are you using RtlRandom instead of RtlRandomEx? 🤨 RtlRandomEx() is an improved version of the RtlRandom() function which is twice as fast and produces better random numbers also.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Jan 27, 2014 02:16 PM)Masm4ever Wrote:  It works... kind of. But its randomness is very bad. Test it with ENT or NIST, RtlRandomEx is really a lousy PRNG.

Here you have some choice.

Are you sure you're seeding it correctly?
You can always just implement your own Markov Chain to create the illusion of random generation.

Just throwing that out there.
(Jan 28, 2014 01:45 AM)_SLQ Wrote:  You can always just implement your own Markov Chain to create the illusion of random generation.

Just throwing that out there.

Hey dude, thanks for bringing that up, I'll look into it when I'm looking at more mathematics algebraic methods in programming
(Jan 27, 2014 11:35 PM)IRP_MJ_SCSI Wrote:  Are you sure you're seeding it correctly?

Pretty sure for my assembler version, but my C++ isn't perfect, so bear with me. I've put together a snippet here, compiles fine with VS 2010 Ex but returns only a 32-bit number, so something must be wrong:
Code
#include <iostream>
#include "Windows.h"

typedef ULONG (__stdcall *pRex) (PULONG seed);

ULONGLONG get_random(ULONG seed)
    {
        pRex RtlRandomEx = (pRex) GetProcAddress(GetModuleHandleA("ntdll.dll"), "RtlRandomEx");
        return RtlRandomEx(&seed)+(0xffffff+1)*seed;  // trying to get a 64-bit number...
    }

int main() {
    using namespace std;
    for (int i = 0; i < 10; i++)
        cout << get_random(i) << endl;
}


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by leetAug 16, 2022 11:36 AM 7 349 Aug 16, 2022 11:36 AM
Last Post: Sajuuk
[C] by MaskinApr 21, 2021 01:52 AM 5 326 Apr 21, 2021 01:52 AM
Last Post: Vector Dynamics
[Python] by Fivepoint0Oct 23, 2018 05:24 PM 3 191 Oct 23, 2018 05:24 PM
Last Post: Fivepoint0