1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2024-11-14 21:03:23 +00:00
LO41/Scripts/generate.pl

61 lines
1.9 KiB
Perl
Raw Normal View History

#!/bin/perl
2018-06-18 14:32:53 +00:00
use strict;
use warnings;
use List::Util qw/shuffle/;
die "Usage : generate.pl agents_number [floor_number]\nagents number range from 2 to 500\n" if @ARGV == 0;
my $total_nb = $ARGV[0];
die "Error : stupid amount of agents ($total_nb)\n" if $total_nb < 2;
my $visitors_nb = int($total_nb / 2);
my $residents_nb = $total_nb - $visitors_nb;
my $floor_number = @ARGV > 2 ? $ARGV[1] : 25;
my $visitors_output_file_name = "visitors.txt";
my $residents_output_file_name = "residents.txt";
2018-06-18 14:32:53 +00:00
my $visitors_db_filename = "visitors.db";
my $residents_db_filename = "residents.db";
my @visitors = ();
my @residents = ();
2018-06-18 14:32:53 +00:00
open(my $visitors_db_file, $visitors_db_filename) or die "Error : no visitors database found \n";
open(my $residents_db_file, $residents_db_filename) or die "Error : no residents database found \n";
while(my $line = <$visitors_db_file>){
chomp($line);
2018-06-18 14:32:53 +00:00
push(@visitors, $line);
}
while(my $line = <$residents_db_file>){
chomp($line);
push(@residents, $line) if(@residents < $residents_nb);
2018-06-18 14:32:53 +00:00
}
die "Database is not big enough for $visitors_nb visitors\n" if @visitors < $visitors_nb;
die "Database is not big enough for $residents_nb residents" if @residents < $residents_nb;
@visitors = shuffle(@visitors);
@residents = shuffle(@residents);
open(my $visitors_output_file, '>', $visitors_output_file_name) or die "Error : couldn't create visitors output file.";
open(my $residents_output_file, '>', $residents_output_file_name) or die "Error : couldn't create residents output file.";
2018-06-18 14:32:53 +00:00
for(my $i = 0; $i < $visitors_nb; $i += 1){
print $visitors_output_file $visitors[$i] . ";" .
$residents[rand $residents_nb] . "\n";
}
for(my $i = 0; $i < $residents_nb; $i += 1){
print $residents_output_file $residents[$i] . ";" .
(int(rand(25)) + 1) . ";" .
(int(rand(25 + 1))) . "\n";
2018-06-18 14:32:53 +00:00
}
close $visitors_db_file;
close $residents_db_file;
close $visitors_output_file;
close $residents_output_file;
2018-06-18 14:32:53 +00:00