• Random Class

    From Jack@Jack@invalid.invalid to comp.lang.c++ on Thu Jul 10 17:19:03 2025
    From Newsgroup: comp.lang.c++

    Simulating a coin flip using C++'s <random> header file.


    /*
    * Compile in Visual studio:
    * cl /EHsc /std:c++20 main.cpp
    */
    #include <iostream>
    #include <random>

    using namespace std;

    int main()
    {
    std::random_device random_device;
    std::mt19937 random_engine{random_device()};
    std::uniform_int_distribution<int> distribution{0, 1};

    int heads = 0; int tails = 0;

    for (int p = 0; p < 10000; p++)
    {
    int coinFlip = distribution(random_engine);

    if (coinFlip == 0)
    heads++;
    else
    tails++;
    }

    cout << "Heads: " << heads << " Tails: " << tails << endl;
    cout << "Heads: " << (double)heads*100/10000 << "% Tails: " << (double)tails*100/10000 << "%" << endl;
    }
    /*
    * Typical output:
    * Heads: 5066 Tails: 4934
    * Heads: 50.66% Tails: 49.34%
    */

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Mr Flibble@flibble@red-dwarf.jmc.corp to comp.lang.c++ on Thu Jul 10 18:00:30 2025
    From Newsgroup: comp.lang.c++

    On Thu, 10 Jul 2025 17:19:03 +0000, Jack wrote:

    Simulating a coin flip using C++'s <random> header file.


    /*
    * Compile in Visual studio:
    * cl /EHsc /std:c++20 main.cpp */
    #include <iostream>
    #include <random>

    using namespace std;

    int main()
    {
    std::random_device random_device;
    std::mt19937 random_engine{random_device()};
    std::uniform_int_distribution<int> distribution{0, 1};

    int heads = 0; int tails = 0;

    for (int p = 0; p < 10000; p++)
    {
    int coinFlip = distribution(random_engine);

    if (coinFlip == 0)
    heads++;
    else
    tails++;
    }

    cout << "Heads: " << heads << " Tails: " << tails << endl; cout <<
    "Heads: " << (double)heads*100/10000 << "% Tails: " << (double)tails*100/10000 << "%" << endl;
    }
    /*
    * Typical output:
    * Heads: 5066 Tails: 4934 * Heads: 50.66% Tails: 49.34%
    */

    std::mt19937 is not cryptograhically secure, don't use it.

    /Flibble
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From scott@scott@slp53.sl.home (Scott Lurndal) to comp.lang.c++ on Thu Jul 10 18:25:46 2025
    From Newsgroup: comp.lang.c++

    Mr Flibble <flibble@red-dwarf.jmc.corp> writes:
    On Thu, 10 Jul 2025 17:19:03 +0000, Jack wrote:

    Simulating a coin flip using C++'s <random> header file.


    /*
    * Compile in Visual studio:
    * cl /EHsc /std:c++20 main.cpp */
    #include <iostream>
    #include <random>

    using namespace std;

    int main()
    {
    std::random_device random_device;
    std::mt19937 random_engine{random_device()};
    std::uniform_int_distribution<int> distribution{0, 1};

    int heads = 0; int tails = 0;

    for (int p = 0; p < 10000; p++)
    {
    int coinFlip = distribution(random_engine);

    if (coinFlip == 0)
    heads++;
    else
    tails++;
    }

    cout << "Heads: " << heads << " Tails: " << tails << endl; cout <<
    "Heads: " << (double)heads*100/10000 << "% Tails: " <<
    (double)tails*100/10000 << "%" << endl;
    }
    /*
    * Typical output:
    * Heads: 5066 Tails: 4934 * Heads: 50.66% Tails: 49.34%
    */

    std::mt19937 is not cryptograhically secure, don't use it.

    It's perfectly suitable for flipping a coin, which cannot
    by any stretch be considered "cryptography".


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Mr Flibble@flibble@red-dwarf.jmc.corp to comp.lang.c++ on Sat Jul 12 11:49:22 2025
    From Newsgroup: comp.lang.c++

    On Thu, 10 Jul 2025 18:25:46 +0000, Scott Lurndal wrote:

    Mr Flibble <flibble@red-dwarf.jmc.corp> writes:
    On Thu, 10 Jul 2025 17:19:03 +0000, Jack wrote:

    Simulating a coin flip using C++'s <random> header file.


    /*
    * Compile in Visual studio:
    * cl /EHsc /std:c++20 main.cpp */
    #include <iostream>
    #include <random>

    using namespace std;

    int main()
    {
    std::random_device random_device;
    std::mt19937 random_engine{random_device()};
    std::uniform_int_distribution<int> distribution{0, 1};

    int heads = 0; int tails = 0;

    for (int p = 0; p < 10000; p++)
    {
    int coinFlip = distribution(random_engine);

    if (coinFlip == 0)
    heads++;
    else
    tails++;
    }

    cout << "Heads: " << heads << " Tails: " << tails << endl; cout
    <<
    "Heads: " << (double)heads*100/10000 << "% Tails: " <<
    (double)tails*100/10000 << "%" << endl;
    }
    /*
    * Typical output:
    * Heads: 5066 Tails: 4934 * Heads: 50.66% Tails: 49.34% */

    std::mt19937 is not cryptograhically secure, don't use it.

    It's perfectly suitable for flipping a coin, which cannot by any stretch
    be considered "cryptography".

    No it isn't.

    /Flibble
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Muttley@Muttley@DastardlyHQ.org to comp.lang.c++ on Sun Jul 13 07:50:52 2025
    From Newsgroup: comp.lang.c++

    On Sat, 12 Jul 2025 11:49:22 GMT
    Mr Flibble <flibble@red-dwarf.jmc.corp> wibbled:
    On Thu, 10 Jul 2025 18:25:46 +0000, Scott Lurndal wrote:

    Mr Flibble <flibble@red-dwarf.jmc.corp> writes:
    On Thu, 10 Jul 2025 17:19:03 +0000, Jack wrote:

    Simulating a coin flip using C++'s <random> header file.


    /*
    * Compile in Visual studio:
    * cl /EHsc /std:c++20 main.cpp */
    #include <iostream>
    #include <random>

    using namespace std;

    int main()
    {
    std::random_device random_device;
    std::mt19937 random_engine{random_device()};
    std::uniform_int_distribution<int> distribution{0, 1};

    int heads = 0; int tails = 0;

    for (int p = 0; p < 10000; p++)
    {
    int coinFlip = distribution(random_engine);

    if (coinFlip == 0)
    heads++;
    else
    tails++;
    }

    cout << "Heads: " << heads << " Tails: " << tails << endl; cout
    <<
    "Heads: " << (double)heads*100/10000 << "% Tails: " <<
    (double)tails*100/10000 << "%" << endl;
    }
    /*
    * Typical output:
    * Heads: 5066 Tails: 4934 * Heads: 50.66% Tails: 49.34% */

    std::mt19937 is not cryptograhically secure, don't use it.

    It's perfectly suitable for flipping a coin, which cannot by any stretch
    be considered "cryptography".

    No it isn't.

    random() % 2 after a suitable seed is sufficient for coin flipping.

    If you're really fussed about random number generation and use linux then
    you can do worse than use /dev/urandom or even better /dev/hwrng which uses
    the hardware rng on the CPU though for some reason seems to be root access only. Wierd.

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Bonita Montero@Bonita.Montero@gmail.com to comp.lang.c++ on Sun Jul 13 10:29:24 2025
    From Newsgroup: comp.lang.c++

    Am 13.07.2025 um 09:50 schrieb Muttley@DastardlyHQ.org:

    random() % 2 after a suitable seed is sufficient for coin flipping.

    If you're really fussed about random number generation and use linux then
    you can do worse than use /dev/urandom or even better /dev/hwrng which uses the hardware rng on the CPU though for some reason seems to be root access only. Wierd.

    Why is /dev/hwrng root-only ? I guess on x86 it bases on the same
    instructions you could access in userspace (RDRAND) without any
    restrictions.

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Muttley@Muttley@DastardlyHQ.org to comp.lang.c++ on Sun Jul 13 08:57:27 2025
    From Newsgroup: comp.lang.c++

    On Sun, 13 Jul 2025 10:29:24 +0200
    Bonita Montero <Bonita.Montero@gmail.com> wibbled:
    Am 13.07.2025 um 09:50 schrieb Muttley@DastardlyHQ.org:

    random() % 2 after a suitable seed is sufficient for coin flipping.

    If you're really fussed about random number generation and use linux then
    you can do worse than use /dev/urandom or even better /dev/hwrng which uses >> the hardware rng on the CPU though for some reason seems to be root access >> only. Wierd.

    Why is /dev/hwrng root-only ? I guess on x86 it bases on the same

    No idea, seems odd. Maybe its just the way its set up on the machine I have access to right now. I'll check on other distros when I get a chance.


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From David Brown@david.brown@hesbynett.no to comp.lang.c++ on Sun Jul 13 14:32:24 2025
    From Newsgroup: comp.lang.c++

    On 13/07/2025 10:29, Bonita Montero wrote:
    Am 13.07.2025 um 09:50 schrieb Muttley@DastardlyHQ.org:

    random() % 2 after a suitable seed is sufficient for coin flipping.

    If you're really fussed about random number generation and use linux then
    you can do worse than use /dev/urandom or even better /dev/hwrng which
    uses
    the hardware rng on the CPU though for some reason seems to be root
    access
    only. Wierd.

    Why is /dev/hwrng root-only ? I guess on x86 it bases on the same instructions you could access in userspace (RDRAND) without any
    restrictions.


    Your guess may or may not be correct - there can be many hardware RNGs,
    many of which are much better than cpu instructions. And on systems
    that use something else for their hardware RNG, root-only access (by
    default) makes sense.

    It may also be to discourage the use of such devices or cpu instructions
    for code that needs cryptographically secure random sources. The
    quality of CPU instruction random numbers has varied significantly
    through the ages, and you'd be na|>ve to rely on it directly.
    /dev/urandom is a better choice for almost anything involving security.

    For coin-tossing, however, pretty much any reasonable pseudo-RNG will be
    fine.


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Bonita Montero@Bonita.Montero@gmail.com to comp.lang.c++ on Sun Jul 13 15:22:39 2025
    From Newsgroup: comp.lang.c++

    Am 13.07.2025 um 14:32 schrieb David Brown:
    On 13/07/2025 10:29, Bonita Montero wrote:
    Am 13.07.2025 um 09:50 schrieb Muttley@DastardlyHQ.org:

    random() % 2 after a suitable seed is sufficient for coin flipping.

    If you're really fussed about random number generation and use linux
    then
    you can do worse than use /dev/urandom or even better /dev/hwrng
    which uses
    the hardware rng on the CPU though for some reason seems to be root
    access
    only. Wierd.

    Why is /dev/hwrng root-only ? I guess on x86 it bases on the same
    instructions you could access in userspace (RDRAND) without any
    restrictions.


    Your guess may or may not be correct - there can be many hardware RNGs,
    many of which are much better than cpu instructions.-a And on systems
    that use something else for their hardware RNG, root-only access (by default) makes sense.

    It may also be to discourage the use of such devices or cpu instructions
    for code that needs cryptographically secure random sources.-a The
    quality of CPU instruction random numbers has varied significantly
    through the ages, and you'd be na|>ve to rely on it directly. /dev/
    urandom is a better choice for almost anything involving security.

    So why is /dev/urandom accessible for anyone if it has comparable
    quality ?

    For coin-tossing, however, pretty much any reasonable pseudo-RNG will be fine.



    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From scott@scott@slp53.sl.home (Scott Lurndal) to comp.lang.c++ on Sun Jul 13 15:19:20 2025
    From Newsgroup: comp.lang.c++

    Muttley@DastardlyHQ.org writes:
    On Sun, 13 Jul 2025 10:29:24 +0200
    Bonita Montero <Bonita.Montero@gmail.com> wibbled:
    Am 13.07.2025 um 09:50 schrieb Muttley@DastardlyHQ.org:

    random() % 2 after a suitable seed is sufficient for coin flipping.

    If you're really fussed about random number generation and use linux then >>> you can do worse than use /dev/urandom or even better /dev/hwrng which uses >>> the hardware rng on the CPU though for some reason seems to be root access >>> only. Wierd.

    Why is /dev/hwrng root-only ? I guess on x86 it bases on the same

    No idea, seems odd. Maybe its just the way its set up on the machine I have >access to right now. I'll check on other distros when I get a chance.


    the rngd daemon reads /dev/hwrng which provides entropy for
    the rngd which provides data to /dev/urandom and /dev/random
    (which augment the HW entropy with kernel entropy). Applications
    should use /dev/urandom or /dev/random.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From scott@scott@slp53.sl.home (Scott Lurndal) to comp.lang.c++ on Sun Jul 13 15:21:22 2025
    From Newsgroup: comp.lang.c++

    Bonita Montero <Bonita.Montero@gmail.com> writes:
    Am 13.07.2025 um 14:32 schrieb David Brown:
    On 13/07/2025 10:29, Bonita Montero wrote:
    Am 13.07.2025 um 09:50 schrieb Muttley@DastardlyHQ.org:

    random() % 2 after a suitable seed is sufficient for coin flipping.

    If you're really fussed about random number generation and use linux
    then
    you can do worse than use /dev/urandom or even better /dev/hwrng
    which uses
    the hardware rng on the CPU though for some reason seems to be root
    access
    only. Wierd.

    Why is /dev/hwrng root-only ? I guess on x86 it bases on the same
    instructions you could access in userspace (RDRAND) without any
    restrictions.


    Your guess may or may not be correct - there can be many hardware RNGs,
    many of which are much better than cpu instructions.-a And on systems
    that use something else for their hardware RNG, root-only access (by
    default) makes sense.

    It may also be to discourage the use of such devices or cpu instructions
    for code that needs cryptographically secure random sources.-a The
    quality of CPU instruction random numbers has varied significantly
    through the ages, and you'd be na|>ve to rely on it directly. /dev/
    urandom is a better choice for almost anything involving security.

    So why is /dev/urandom accessible for anyone if it has comparable
    quality ?

    1) /dev/urandom has been the go-to source for random numbers in linux
    for decades. Its output is derived from both hardware entropy and
    the kernel entropy pool.

    2) See prior reply vis-a-vis /dev/hwrng and rngd(1m).
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From scott@scott@slp53.sl.home (Scott Lurndal) to comp.lang.c++ on Sun Jul 13 15:22:27 2025
    From Newsgroup: comp.lang.c++

    Muttley@DastardlyHQ.org writes:
    On Sun, 13 Jul 2025 14:32:24 +0200
    David Brown <david.brown@hesbynett.no> wibbled:
    that use something else for their hardware RNG, root-only access (by >>default) makes sense.
    It may also be to discourage the use of such devices or cpu instructions >>for code that needs cryptographically secure random sources. The
    quality of CPU instruction random numbers has varied significantly
    through the ages, and you'd be na|>ve to rely on it directly.
    /dev/urandom is a better choice for almost anything involving security.

    urandom is most definately not as secure as hwrng as its purely software >driven with all the issues that entails.

    That is not the case. urandom output is derived from multiple entropy
    pools, including hwrng if available.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Chris M. Thomasson@chris.m.thomasson.1@gmail.com to comp.lang.c++ on Sun Jul 13 12:56:50 2025
    From Newsgroup: comp.lang.c++

    On 7/13/2025 8:19 AM, Scott Lurndal wrote:
    Muttley@DastardlyHQ.org writes:
    On Sun, 13 Jul 2025 10:29:24 +0200
    Bonita Montero <Bonita.Montero@gmail.com> wibbled:
    Am 13.07.2025 um 09:50 schrieb Muttley@DastardlyHQ.org:

    random() % 2 after a suitable seed is sufficient for coin flipping.

    If you're really fussed about random number generation and use linux then >>>> you can do worse than use /dev/urandom or even better /dev/hwrng which uses
    the hardware rng on the CPU though for some reason seems to be root access >>>> only. Wierd.

    Why is /dev/hwrng root-only ? I guess on x86 it bases on the same

    No idea, seems odd. Maybe its just the way its set up on the machine I have >> access to right now. I'll check on other distros when I get a chance.


    the rngd daemon reads /dev/hwrng which provides entropy for
    the rngd which provides data to /dev/urandom and /dev/random
    (which augment the HW entropy with kernel entropy). Applications
    should use /dev/urandom or /dev/random.

    Fwiw, on a side note, check this shit out:

    (A funny race-condition based RNG...) https://groups.google.com/g/comp.lang.c++/c/7u_rLgQe86k/m/fYU9SnuAFQAJ

    ;^)
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From David Brown@david.brown@hesbynett.no to comp.lang.c++ on Mon Jul 14 16:06:12 2025
    From Newsgroup: comp.lang.c++

    On 13/07/2025 15:22, Bonita Montero wrote:
    Am 13.07.2025 um 14:32 schrieb David Brown:
    On 13/07/2025 10:29, Bonita Montero wrote:
    Am 13.07.2025 um 09:50 schrieb Muttley@DastardlyHQ.org:

    random() % 2 after a suitable seed is sufficient for coin flipping.

    If you're really fussed about random number generation and use linux
    then
    you can do worse than use /dev/urandom or even better /dev/hwrng
    which uses
    the hardware rng on the CPU though for some reason seems to be root
    access
    only. Wierd.

    Why is /dev/hwrng root-only ? I guess on x86 it bases on the same
    instructions you could access in userspace (RDRAND) without any
    restrictions.


    Your guess may or may not be correct - there can be many hardware
    RNGs, many of which are much better than cpu instructions.-a And on
    systems that use something else for their hardware RNG, root-only
    access (by default) makes sense.

    It may also be to discourage the use of such devices or cpu
    instructions for code that needs cryptographically secure random
    sources.-a The quality of CPU instruction random numbers has varied
    significantly through the ages, and you'd be na|>ve to rely on it
    directly. /dev/ urandom is a better choice for almost anything
    involving security.

    So why is /dev/urandom accessible for anyone if it has comparable
    quality ?


    /dev/urandom provides a higher quality source of random numbers than
    most hardware random number generators - especially the simplistic ones
    found in some cpus. It is also documented (with the source available, obviously), and analysed by independent experts - unlike cpu random
    number generators which could be buggy, have unexpected biases or uneven distributions, or even be compromised with backdoors or predictable
    sequences (extreme paranoia is a virtue for cryptographers).

    (Muttley is wrong to suggest that /dev/hwrng is better than
    /dev/urandom, in almost all situations.)


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From David Brown@david.brown@hesbynett.no to comp.lang.c++ on Mon Jul 14 16:19:15 2025
    From Newsgroup: comp.lang.c++

    On 13/07/2025 16:59, Muttley@DastardlyHQ.org wrote:
    On Sun, 13 Jul 2025 14:32:24 +0200
    David Brown <david.brown@hesbynett.no> wibbled:
    that use something else for their hardware RNG, root-only access (by
    default) makes sense.
    It may also be to discourage the use of such devices or cpu instructions
    for code that needs cryptographically secure random sources. The
    quality of CPU instruction random numbers has varied significantly
    through the ages, and you'd be na|>ve to rely on it directly.
    /dev/urandom is a better choice for almost anything involving security.

    urandom is most definately not as secure as hwrng as its purely software driven with all the issues that entails.


    No.

    /dev/hwrng gets its values from some kind of hardware random number
    generator. Some hardware number generators are good, some are bad.
    Some have poor distributions that are hard to use well. Some have
    questions about their security. Some are very slow.

    /dev/urandom makes use of /dev/hwrng as a source of entropy for seeding
    a high quality pseudo random number generator. It also uses other
    entropy sources such as timing of network packets or user interaction, temperature fluctuations, and so on. This makes it immune to any risk
    of backdoors or deliberately poor randomness. The PRNG algorithms have clearer and smoother distributions - while keeping the entropy that the
    system gets in. (If you pull vast amounts out of /dev/urandom, in
    relation to the entropy it gets, you will reduce the quality of randomness.)

    And of course the source for /dev/urandom is there for anyone to see and analyse, unlike the black boxes of hardware random number generators.



    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From candycanearter07@candycanearter07@candycanearter07.nomail.afraid to comp.lang.c++ on Mon Jul 14 15:10:02 2025
    From Newsgroup: comp.lang.c++

    Scott Lurndal <scott@slp53.sl.home> wrote at 15:22 this Sunday (GMT):
    Muttley@DastardlyHQ.org writes:
    On Sun, 13 Jul 2025 14:32:24 +0200
    David Brown <david.brown@hesbynett.no> wibbled:
    that use something else for their hardware RNG, root-only access (by >>>default) makes sense.
    It may also be to discourage the use of such devices or cpu instructions >>>for code that needs cryptographically secure random sources. The >>>quality of CPU instruction random numbers has varied significantly >>>through the ages, and you'd be na|>ve to rely on it directly. >>>/dev/urandom is a better choice for almost anything involving security.

    urandom is most definately not as secure as hwrng as its purely software >>driven with all the issues that entails.

    That is not the case. urandom output is derived from multiple entropy pools, including hwrng if available.


    If you're that worried, you can use a third party library on top of
    that.
    --
    user <candycane> is generated from /dev/urandom
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Bonita Montero@Bonita.Montero@gmail.com to comp.lang.c++ on Mon Jul 14 17:38:27 2025
    From Newsgroup: comp.lang.c++

    Am 14.07.2025 um 16:06 schrieb David Brown:

    /dev/urandom provides a higher quality source of random numbers than
    most hardware random number generators - especially the simplistic ones found in some cpus.-a It is also documented (with the source available, obviously), and analysed by independent experts - unlike cpu random
    number generators which could be buggy, have unexpected biases or uneven distributions, or even be compromised with backdoors or predictable sequences (extreme paranoia is a virtue for cryptographers).

    Show me a source for that regarding RDRAND on x86.

    --- Synchronet 3.21a-Linux NewsLink 1.2