Randomize a pgn file

Discussion of anything and everything relating to chess playing software and machines.

Moderators: hgm, Dann Corbit, Harvey Williamson

User avatar
PeterO
Posts: 211
Joined: Sun Jul 31, 2016 6:35 pm

Randomize a pgn file

Post by PeterO »

Hello,

I have a database with chessproblems in pgn. Ordered by topics.
Do,you know a tool - to randomize the order of of the game in the pgn?

Peter
User avatar
phhnguyen
Posts: 1431
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

Re: Randomize a pgn file

Post by phhnguyen »

it is an easy task. Just compile and run a C++ code as below:

Code: Select all

//
//  ShufflePgn
//
//  Created by Nguyen Pham on 24/7/2022.
//

#include <iostream>
#include <fstream>
#include <vector>
#include <random>
#include <algorithm>
#include <iterator>

int main(int argc, const char * argv[])
{
    std::cout << "shuffle games of a pgn file" << std::endl;
        
    std::string inpath, outpath;

    // arguments
    {
        if (argc < 2) {
            std::cerr << "Usage: shufflepgn INFILE OUTFILE" << std::endl;
            return -1;
        }

        inpath = std::string(argv[1]);
        outpath = std::string(argv[2]);
    }

    std::vector<std::string> pgnVec;
    
    // read in
    {
        std::ifstream file(inpath);
        if (!file.is_open()) {
            return -1;
        }
        
        std::string pgn, line;
        auto cnt = 0;
        while (std::getline(file, line)) {
            auto isEvenLine = line.find("[Event") != std::string::npos;
            if (isEvenLine) {
                if (cnt) {
                    pgnVec.push_back(pgn);
                }
                pgn.clear();
                cnt = 0;
            }
            
            pgn += line + "\n";
            if (!line.empty()) {
                cnt++;
            }
        }
        file.close();
        
        if (cnt) {
            pgnVec.push_back(pgn);
        }
    }
    
    if (pgnVec.size() <= 1) {
        std::cerr << "Error: cannot read input file or it has only or under a game" << std::endl;
        return -1;
    }
    
    // shuffle games
    {
        std::random_device rd;
        std::mt19937 g(rd());
     
        std::shuffle(pgnVec.begin(), pgnVec.end(), g);
    }
    
    // write out
    {
        std::ofstream out(outpath);
        
        for(auto && pgn : pgnVec) {
            out << pgn << "\n";
        }
        out.close();
    }

    return 0;
}
Usage:

Code: Select all

shufflepgn INPUTPATH OUTPUTPATH
https://banksiagui.com
The most features chess GUI, based on opensource Banksia - the chess tournament manager
User avatar
pohl4711
Posts: 2388
Joined: Sat Sep 03, 2011 7:25 am
Location: Berlin, Germany
Full name: Stefan Pohl

Re: Randomize a pgn file

Post by pohl4711 »

PeterO wrote: Sun Jul 24, 2022 10:19 am Hello,

I have a database with chessproblems in pgn. Ordered by topics.
Do,you know a tool - to randomize the order of of the game in the pgn?

Peter
Scid
Sort Database, choose random.
User avatar
PeterO
Posts: 211
Joined: Sun Jul 31, 2016 6:35 pm

Re: Randomize a pgn file

Post by PeterO »

Thank you VERY MUCH! :D :)

Peter