Results 1 to 2 of 2

Thread: random number generator average displayed on screen

  1. #1
    Join Date
    Aug 2015
    Beans
    2

    random number generator average displayed on screen

    Greetings all,
    I apologize if my request seems trivial but I am looking for a way to run a random number generator vis script: 0s and 1s only and have it display a running average onscreen till I Ctrl-C to stop it.

    I can run "rand -N 100000 -M 2 -e -d '\n' > random.txt" and generate an infinite list if needed then import into libreoffice calc to evaluate but I'd rather just watch a running average displayed on a black screen till I stop it from running.

    I'm not a programmer so I thought I'd give the forum a change to help (or flame me for not doing it myself).
    Thanks in advance.

    M

  2. #2
    Join Date
    Nov 2009
    Location
    Texas
    Beans
    59
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: random number generator average displayed on screen

    Odd request but sure, I'll oblige.

    Code:
    #include <iostream>
    #include <random>
    #include <chrono>
    
    int main() {
        double sum = 0;
        double its = 0;
    
        while(true) {
            std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
            std::uniform_int_distribution<int> dist(0, 1);
            sum += dist(generator);
            double avg = sum/++its;
            std::cout << avg << "\n";
        }
    
    }
    save that as "random.cpp" and just compile that with g++

    Code:
    g++ random.cpp -o random
    Then add the following to your shell script:

    Code:
    ./random

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •