Here's a funny program that simulates the matrix...waaay off topic...

#include <iostream>
#include <ctime>

using namespace std;

const int NotUsed=system("color 02");

int main()
{
    clock();
    int n;
    double time;
    char matrixwords[]="  aijk FGHIJKlmnopqrs  ABCDtuvELMNO  PQRS bcde fghTUVWXYwxyzZ";
    char matrix[]="01001100100101011110";
    
    clock();
    for(int o=1; o<=70; o++)
    {
            cout << "/";
            }
            cout << "\n\n\t\t\tThe Matrix\n\n";
    for(int o=1; o<=70; o++)
    {
            cout << "/";
            }
            cout << "\n\n\tEnter Matrix ID: (KB's of Data) ::==:: ";
            cin >> n;
            for(double t=-1000; t<=1000000; t++);
            cout << "\n\n\tLoading Matrix Engine.";
            for(double t=0; t<=5; t++)
            {
                       for(double u=-10000; u<=10000000; u++);
                       cout << " . ";
                       }
                       time=clock()/1000.0;
                       cout << "\n\n\t\tYou waited " << time << " seconds for the matrix to generate\n\n\t";
    
    for(int i=1; i<=n; i++)
    {
            for(int j=1; j<=60; j++)
            {
                    if((j%2==0) || (j%5==0) || (j%13==0))
                    {
                              int ran( rand()%21 );
                              cout << matrix[ran];
                              }
                              else if(j%3==0)
                              {
                                   int ran( rand()%66 );
                                  cout << matrixwords[ran];
                                  }
                                  else if(j%7==0)
                                  {
                                       int ran( rand()%66 );
                                       cout << matrixwords[ran];
                                       }
                                  else
                                  {
                                      int ran( rand()%66 );
                                      cout << matrixwords[ran];
                                      }
                    }
                    cout << "\n\t";
            }
            time+=clock()/1000.0;
    
    cout << "\n\n\tTotal Time Taken for the Matrix to Run: " << time << " s\n\n\t";
    cin.get();
    return 0;
}

programmed in c++.. kinda dumb..but still

Dani AI

Generated

Nice little toy from and fun reactions from , and others. A few practical notes that turn this demo into a safer, more portable snippet and explain the common pitfalls people hit when they copy-and-run "Matrix" demos.

  • Avoid side effects during static initialization (calling system(...) in a global initializer). Do setup in main so startup ordering and errors are clearer.
  • Don’t busy-wait for timing (for loops that just increment a double). Use std::this_thread::sleep_for for delays and std::chrono::steady_clock for timing.
  • Prefer <random> (std::mt19937 + uniform_int_distribution) over rand() and avoid using % with an incorrectly computed modulus — off-by-one or zero-length strings cause undefined behavior.
  • Buffer each line into a std::string and write it once (lots faster than calling operator<< per character). For color/clear, use ANSI escapes or a proper console library rather than system("cls")/system("clear").

Example (modern C++11+) — compact, safer matrix effect:

#include <iostream>
#include <random>
#include <string>
#include <chrono>
#include <thread>

int main() {
    const std::string chars = "  aijkFGHIJKlmnopqrs  ABCDtuvELMNO  PQRS bcde fghTUVWXYwxyzZ01";
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<std::size_t> dist(0, chars.size() - 1);

    for (int line = 0; line < 200; ++line) {
        std::string out;
        out.reserve(60);
        for (int col = 0; col < 60; ++col) out.push_back(chars[dist(gen)]);
        std::cout << out << '\n';
        std::this_thread::sleep_for(std::chrono::milliseconds(40));
    }
}

Troubleshooting: if the program appears to generate the same output each run, check the seed (random_device may be deterministic on some platforms); if it crashes, verify the character buffer length before building the distribution; if colors do not show, enable ANSI processing on Windows or use a cross-platform terminal library. This keeps the effect visually similar to the original while removing undefined behavior, improving timing accuracy, and making the code easier to maintain.

Recommended Answers

All 11 Replies

Yes, it's a severe blow for all C++-lovers...
And your question is...

Compiled id, kindda funny ya, though...
Not really usefull to be honest :P

Yes, it's a severe blow for all C++-lovers...
And your question is...

No question...I just thought it was kinda funny...it was in another forum, but got moved here...

Compiled id, kindda funny ya, though...
Not really usefull to be honest :P

Yeah...there is no point in the code...it just simulates the matrix

commented: stoopid sig -3

Well, in that case a free advice for a C++ coder: look at the C++ language definition. Must be int main() , not improbable void main() in signature ;)...

Member Avatar for Member #46692

Whatever it is, it looks pretty lame and probably could be improved with a clearscreen function that is system dependent.

Well, in that case a free advice for a C++ coder: look at the C++ language definition. Must be int main() , not improbable void main() in signature ;)...

Yeah...I know about that but int main() would need a return statement, which would make my signature 6 lines, one more than is allowed... and anyway, its only just a signature!

Reminds me of the grandaddy of all screensavers. Something like

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main()
  {
  srand( time( NULL ) );
  while (1) printf( "%*c", rand() % 15, '*' );
  return 0;
  }

(This, of course, is just the simplest possible version...)

Thank you, Duodas, for this brilliant code!
It's so pity it's not a part of the C RTL...

You gotta admit, the Classics are good!

Yeah...I know about that but int main() would need a return statement, which would make my signature 6 lines, one more than is allowed... and anyway, its only just a signature!

Good news for amrith92: The C++ Standard does not require explicit return statement in int main() !

Good news for amrith92: The C++ Standard does not require explicit return statement in int main() !

Hear, Hear! So this means my Signature is valid!! Anyways, it isn't a standard and I would urge myself never to comment on it again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.