The C language implements an example of an algorithm that randomly arranges the contents of a file by line

  • 2020-05-30 20:35:35
  • OfStack

This article illustrates an algorithm to randomly arrange the contents of files by lines in C language. I will share it with you for your reference as follows:

In practice, there is a requirement to randomly extract 1 part from a given set of data.

One simple method is to select one from several rows according to the total number of data bars and the number of data bars to be extracted, so as to achieve the purpose of randomly extracting part 1.

But in this way, the source data is sequential, then the extracted data is also sequential, which does not satisfy some scenarios.

The function that realizes here is: will all data, arrange again by the row randomly, so choose a few rows from the result header, it is a few randomly selected rows, more convenient.

Implementation idea: for the data of N rows, mark each row with the number not repeated between [1-N], and then arrange them according to the number of marks. (it takes a little more effort not to repeat)

The implementation idea is more important, the implementation is simple ~

Implementation of c combined with shell, the following is the reference code.

General control script: mark with random Numbers that do not repeat, then sort by mark

### note: sh in_fname out_fname ###
line_num=`cat $infile | wc -l `
./random $line_num $infile $outfile.tmp
sort $outfile.tmp -k 2 -n -t ' ' | cut -f1 > $outfile

Randomize the implementation of random

#include <string>
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
const int LEN = 4098;
// Return to belong to [p,q) Random integer of 
int rand(int p, int q)
   int size = q-p+1;
   return p+ rand()%size;
// Swap two element values 
void swap(int& a , int& b)
   int temp = a;
   a = b;
   b = temp;
// Print array value 
void print(int *v, int n)
    for(int i=0; i < n ; i++)
        printf("%u\n", v[i]);
// To the array a[n],  Random unrepeated assignments [1,n] Between the number of 
void randomize(int *v, int n)
    for(int i=0; i < n; i++)
        v[i] = i+1;
    for(int i=n-1; i>0; i--)
        int r = rand(0,i+1);
        swap(v[r], v[i]);
// Delete the newline character 
int chomp(char *str)
  int len = strlen(str);
  while(len > 0 && (str[len - 1] == '\n' || str[len - 1] == '\r'))
    str[len - 1] = 0;
  return len;
// The main function 
int main(int argc, char *argv[])
    int line_num = atoi(argv[1]);
    int *value = (int*)malloc((line_num) * sizeof(int));
    randomize(value, line_num);
    //print(value, N);
    FILE* infile = fopen(argv[2], "r");
    if( infile == NULL )
        printf("Cann't open file %s.", argv[1]);
        return 0;
    FILE* outfile = fopen(argv[3], "w");
    if( outfile == NULL)
        printf("Cann't open file %s to write.", argv[2]);
        return 0;
    int i=0;
    char str[LEN];
    str[0] = 0;
    str[LEN-1] = 0;
    while( !feof(infile) )
        if( !fgets(str, sizeof(str),infile))
        str[LEN- 1] = 0;
        fprintf(outfile, "%s\t%u\n", str, value[i]);
    return 0;

I hope this article has been helpful to you in programming the C language.

Related articles: