Skip to content

Fix #737, with random_init of distribution_safe_integral - #738

Open
JAuriac wants to merge 1 commit into
mainfrom
737_IntegralRandomInit
Open

Fix #737, with random_init of distribution_safe_integral#738
JAuriac wants to merge 1 commit into
mainfrom
737_IntegralRandomInit

Conversation

@JAuriac

@JAuriac JAuriac commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

From #735.
For exactly the integer types std::uniform_int_distribution permits, as its IntType parameter (short, int, long, long long, and unsigned variants). bool and char-like types are intentionally excluded as uniform_int_distribution is undefined behavior for those, and this file doesn't provide an alternative for them. A struct containing a bool or char member will fail to compile here rather than compile into a runtime undefined behavior.
To handle bool and char, the following would be needed :

// bool
static inline void random_init(std::uniform_random_bit_generator auto& gen, std::same_as<bool> auto& t)
{
	t = std::bernoulli_distribution(0.5)(gen);
}

// char
static inline void random_init(std::uniform_random_bit_generator auto& gen, narrow_integral auto& t)
{
	using T = std::remove_reference_t<decltype(t)>;
	using Wide = std::conditional_t<std::is_signed_v<T>, int, unsigned int>;
	t = static_cast<T>(std::uniform_int_distribution<Wide>(std::numeric_limits<T>::min(), std::numeric_limits<T>::max())(gen));
}

List of things to check before making a PR

Before merging your code, please check the following:

  • you have added a line describing your changes to the Changelog;
  • you have added unit tests for any new or improved feature;
  • in case you updated dependencies, you have checked pdi/docs/CheckList.md;
  • in case of a change in pdi.h, this same change must be reflected in mock_pdi/pdi.h;
  • in case of a new plugin, make sure the plugin issues the corresponding timer events;
  • you have checked your code format:
    • you have checked that you respect all conventions specified in CONTRIBUTING.md;
    • you have checked that the indentation and formatting conforms to the .clang-format;
    • you have documented with doxygen any new or changed function / class;
  • you have correctly updated the copyright headers:
    • your institution is in the copyright header of every file you (substantially) modified;
    • you have checked that the end-year of the copyright there is the current one;
  • you have updated the AUTHORS file:
    • you have added yourself to the AUTHORS file;
    • if this is a new contribution, you have added it to the AUTHORS file;
  • you have added everything to the user documentation:
    • any new CMake configuration option;
    • any change in the yaml config;
    • any change to the public or plugin API;
    • any other new or changed user-facing feature;
    • any change to the dependencies;
  • you have correctly linked your MR to one or more issues:
    • your MR solves an identified issue;
    • your commit contain the Fix #issue keyword to autoclose the issue when merged.

@JAuriac
JAuriac requested a review from a team August 4, 2026 08:18
@JAuriac
JAuriac force-pushed the 737_IntegralRandomInit branch from 1cb7396 to 82bc122 Compare August 4, 2026 08:23
@JAuriac
JAuriac force-pushed the 737_IntegralRandomInit branch from 82bc122 to 55a2a35 Compare August 4, 2026 08:26
@JAuriac
JAuriac requested a review from jmorice91 August 4, 2026 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant