Here's my ported C++ code for the PowerShell 5.0 PRNG, if it's of any use to anyone else searching. Confirmed it produces the same numbers as PowerShell 5.1 on Windows 10.
It utilizes my Random
class that is a ported version of the .NET RNG, which I separated a bit to make both inherit from a common interface (Random.h
) and renamed to RandomDotNet
: https://stackoverflow.com/a/39338606/1301139
Random.h
#include <limits>
#include <Windows.h>
#pragma once
class Random
{
public:
virtual ~Random() {}
virtual int Next() = 0;
virtual int Next(int minValue, int maxValue) = 0;
virtual int Next(int maxValue) = 0;
virtual void NextBytes(BYTE *buffer, int bufferLen) {};
virtual double NextDouble() = 0;
};
RandomPS5.h
#include <limits>
#include <Windows.h>
#include "Random.h"
#pragma once
class RandomPS5 : public Random
{
protected:
double InternalSampleLargeRange();
int InternalSample();
int BytesToInt(BYTE *dword);
Random *pseudoGenerator;
public:
RandomPS5(int seed);
~RandomPS5();
int Next();
int Next(int minValue, int maxValue);
int Next(int maxValue);
double NextDouble();
void NextBytes(BYTE *buffer, int bufferLen);
};
RandomPS5.cpp
#include "stdafx.h"
#include "RandomPS5.h"
#include "RandomDotNet.h"
#include <limits.h>
#include <math.h>
#include <stdexcept>
#include <string>
// Naive conversion of BitConverter.ToInt32
int RandomPS5::BytesToInt(BYTE *b) {
int Int32 = 0;
Int32 = (Int32 << 8) + b[3];
Int32 = (Int32 << 8) + b[2];
Int32 = (Int32 << 8) + b[1];
Int32 = (Int32 << 8) + b[0];
return Int32;
}
RandomPS5::RandomPS5(int seed) {
pseudoGenerator = new RandomDotNet(seed);
}
RandomPS5::~RandomPS5(){
delete pseudoGenerator;
}
double RandomPS5::NextDouble() {
return Next() * (1.0 / 0x7FFFFFFF);
}
int RandomPS5::Next() {
int result;
do {
result = InternalSample();
} while (result == 0x7FFFFFFF);
if (result < 0) {
result += 0x7FFFFFFF;
}
return result;
}
int RandomPS5::Next(int maxValue) {
if (maxValue<0) {
throw std::invalid_argument("maxValue must be positive");
}
return Next(0, maxValue);
}
int RandomPS5::Next(int minValue, int maxValue) {
if (minValue > maxValue)
{
throw std::invalid_argument("minValue is larger than maxValue");
}
long range = (long)maxValue - (long)minValue;
if (range <= 0x7FFFFFFF)
{
return ((int)(NextDouble() * range) + minValue);
}
else
{
double largeSample = this->InternalSampleLargeRange() * (1.0 / (2 * 0x7FFFFFFF));
int result = (int)((long)(largeSample * range) + minValue);
return result;
}
}
int RandomPS5::InternalSample() {
BYTE *data = (BYTE*)malloc(sizeof(int));
this->NextBytes(data, sizeof(int));
int result = BytesToInt(data);
free(data);
return result;
}
double RandomPS5::InternalSampleLargeRange() {
double result;
do{
result = this->InternalSample();
} while (result == 0x7FFFFFFF);
result += 0x7FFFFFFF;
return result;
}
void RandomPS5::NextBytes(BYTE *buffer, int bufferLen) {
this->pseudoGenerator->NextBytes(buffer, bufferLen);
}
Main.cpp
#include "RandomDotNet.h"
#include "RandomPS5.h"
#include <Windows.h>
// Length of charset string
#define CHARSETLEN 62
// Random charset
const char charset[CHARSETLEN + 1] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
// Function that processes a record like PowerShell does for -ObjectList
void processRecord(CHAR *string, int len, Random *r) {
// Processed characters
int processed = 0;
int i, indexToReplace;
CHAR temp;
// Iterate the charset
for (i = 0; i < CHARSETLEN; ++i) {
if (processed < len) {
string[processed] = charset[i];
}
else if (r->Next(processed + 1) < len) {
string[r->Next(len)] = charset[i];
}
++processed;
}
// Iterate selected items to return them in "random" order
for (i = 0; i < len; ++i) {
// Get random index
indexToReplace = r->Next(i, len);
if (i != indexToReplace) {
// Swap
temp = string[i];
string[i] = string[indexToReplace];
string[indexToReplace] = temp;
}
}
// Terminate the string
string[len] = '\0';
}
int main(int argc, char* argv[]){
// Example usage with a given seed
Random *r = new RandomPS5(1000);
// Length of random string
int len = 49;
// Random string buffer
CHAR *buffer = (CHAR*)malloc(len + 1);
// ([char[]](Get-Random -Input $(48..57 + 65..90 + 97..122) -Count 49 -SetSeed 1000)) -Join ""
processRecord(buffer, len, r);
// Produces: y6FLfcKrpINqgP25GXS7Z0dVBmJOzntlQ3hjbHMAU1ExkewWY
printf("Random string: %s", buffer);
delete r;
return 0;
}
Random
class isn't even the same across all platforms. You should not be trying to duplicate behavior in a component that itself does not have a documented algorithm it promises to use. If you want code in PowerShell and C# to both generate random numbers identically, you need to implement the generator in both languages yourself.Random
being different across platforms before in my experience - that could definitely be the root of my issue here.Random
class mentions several times that the implementation is changeable across .NET versions. For example: "However, note that Random objects in processes running under different versions of the .NET Framework may return different series of random numbers even if they're instantiated with identical seed values.".Random.Next
. Instead it is usingNextBytes
which it then converts to an integer.