Skip to content

Commit

Permalink
xoshiro RNG tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
dashodanger committed Jun 23, 2024
1 parent 3af5087 commit 694b9dc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 28 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ add_executable(
${OBSIDIAN_SOURCE_FILES}
)

target_include_directories(obsidian SYSTEM PRIVATE libraries/fastPRNG)
if(NOT CONSOLE_ONLY)
target_include_directories(obsidian SYSTEM PRIVATE libraries/fltk)
target_include_directories(
Expand Down
54 changes: 33 additions & 21 deletions source/sys_xoshiro.cc
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
/*
Xoshiro256 Random Generator
By Dashodanger, 2020
This is meant to be a replacement for the AJ_Random library that
uses the fastPRNG xoshiro256 implementation for random number generation.
Usage will be as similar to AJ_Random as possible in order to minimize
changes in other sections of code.
*/

#include "../fastPRNG/fastPRNG.h"
//------------------------------------------------------------------------
// RANDOM NUMBER GENERATION (Xoshiro256)
//------------------------------------------------------------------------
//
// OBSIDIAN Level Maker
//
// Copyright (C) 2020-2024 The OBSIDIAN Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//------------------------------------------------------------------------

#include "fastPRNG.h"

fastPRNG::fastXS64 xoshiro;

Expand All @@ -20,22 +29,25 @@ void xoshiro_Reseed(uint64_t newseed)

uint64_t xoshiro_UInt()
{
int64_t rand_num = (int64_t)(xoshiro.xoshiro256p());
if (rand_num >= 0)
{
return rand_num;
}
return -rand_num;
return xoshiro.xoshiro256p();
}

float xoshiro_Float()
{
return xoshiro.xoshiro256p_UNI<float>();
}

double xoshiro_Double()
{
return xoshiro.xoshiro256p_UNI<double>();
}

// This probably isn't super efficient, but it is rarely used and shouldn't make
// a huge overall hit to performance - Dasho
int xoshiro_Between(int low, int high)
{
return (int)(xoshiro.xoshiro256p_Range<float>(low, high));
return (int)xoshiro.xoshiro256p_Range<float>(low, high);
}

double xoshiro_Between(double low, double high)
{
return xoshiro.xoshiro256p_Range<double>(low, high);
}
27 changes: 22 additions & 5 deletions source/sys_xoshiro.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
// Xoshiro256 Random Generator
//------------------------------------------------------------------------
// RANDOM NUMBER GENERATION (Xoshiro256)
//------------------------------------------------------------------------
//
// OBSIDIAN Level Maker
//
// Copyright (C) 2020-2024 The OBSIDIAN Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//------------------------------------------------------------------------

#pragma once

#include "../../libraries/fastPRNG/fastPRNG.h"

extern fastPRNG::fastXS64 xoshiro;

void xoshiro_Reseed(uint64_t newseed);

uint64_t xoshiro_UInt();

// These return in the range of 0.0f-1.0f/0.0-1.0
float xoshiro_Float();
double xoshiro_Double();

int xoshiro_Between(int low, int high);
double xoshiro_Between(double low, double high);
4 changes: 2 additions & 2 deletions source/ui_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ void UI_Module::randomize_Values(std::vector<std::string> selected_randomize_gro
M->nan_options->do_callback();
}
M->mod_slider->value(M->mod_slider->round(
xoshiro.xoshiro256p_Range<double>(M->mod_slider->minimum(), M->mod_slider->maximum())));
xoshiro_Between(M->mod_slider->minimum(), M->mod_slider->maximum())));
M->mod_slider->do_callback();
break;
}
Expand All @@ -610,7 +610,7 @@ void UI_Module::randomize_Values(std::vector<std::string> selected_randomize_gro
{
if (StringCompare(group, M->randomize_group) == 0)
{
if (xoshiro.xoshiro256p_UNI<float>() < 0.5)
if (xoshiro_Float() < 0.5)
{
M->mod_check->value(0);
}
Expand Down

0 comments on commit 694b9dc

Please sign in to comment.