
Signed-off-by: Jun Sun <jsun@junsun.net>
---
 risugen                |  4 ++++
 risugen_arm.pm         | 11 ++++++++---
 risugen_loongarch64.pm |  9 +++++++--
 risugen_m68k.pm        |  9 +++++++--
 risugen_ppc64.pm       | 10 +++++++---
 5 files changed, 33 insertions(+), 10 deletions(-)

diff --git a/risugen b/risugen
index fa94a39..360112f 100755
--- a/risugen
+++ b/risugen
@@ -293,6 +293,7 @@ and outputfile is the generated raw binary file.
 
 Valid options:
     --numinsns n : generate n instructions (default is 10000)
+    --randseed n : use n as random generator seed (default uses time())
     --fpscr n    : set initial FPSCR (arm) or FPCR (aarch64) value (default is 0)
     --condprob p : [ARM only] make instructions conditional with probability p
                    (default is 0, ie all instructions are always executed)
@@ -317,6 +318,7 @@ EOT
 sub main()
 {
     my $numinsns = 10000;
+    my $randseed=-1;
     my $condprob = 0;
     my $fpscr = 0;
     my $fp_enabled = 1;
@@ -326,6 +328,7 @@ sub main()
 
     GetOptions( "help" => sub { usage(); exit(0); },
                 "numinsns=i" => \$numinsns,
+                "randseed=i" => \$randseed,
                 "fpscr=o" => \$fpscr,
                 "group=s" => \@groups,
                 "pattern=s" => \@pattern_re,
@@ -365,6 +368,7 @@ sub main()
         'condprob' => $condprob,
         'fpscr' => $fpscr,
         'numinsns' => $numinsns,
+        'randseed' => $randseed,
         'fp_enabled' => $fp_enabled,
         'sve_enabled' => $sve_enabled,
         'outfile' => $outfile,
diff --git a/risugen_arm.pm b/risugen_arm.pm
index 28ca72f..2dc144d 100644
--- a/risugen_arm.pm
+++ b/risugen_arm.pm
@@ -1069,7 +1069,7 @@ sub gen_one_insn($$)
     }
 }
 
-sub write_test_code($$$$$$$$)
+sub write_test_code($$$$$$$$$)
 {
     my ($params) = @_;
 
@@ -1090,6 +1090,7 @@ sub write_test_code($$$$$$$$)
     my $condprob = $params->{ 'condprob' };
     my $fpscr = $params->{ 'fpscr' };
     my $numinsns = $params->{ 'numinsns' };
+    my $randseed = $params->{ 'randseed' };
     my $fp_enabled = $params->{ 'fp_enabled' };
     my $sve_enabled = $params->{ 'sve_enabled' };
     my $outfile = $params->{ 'outfile' };
@@ -1103,8 +1104,12 @@ sub write_test_code($$$$$$$$)
     # probability of forcing insn to unconditional
     $condprob = 1 - $condprob;
 
-    # TODO better random number generator?
-    srand(0);
+    # use time() as random gen seed by default
+    if ($randseed == -1) {
+        srand(time());
+    } else {
+        srand($randseed);
+    }
 
     print "Generating code using patterns: @keys...\n";
     progress_start(78, $numinsns);
diff --git a/risugen_loongarch64.pm b/risugen_loongarch64.pm
index 5394fdc..3b1b4f9 100644
--- a/risugen_loongarch64.pm
+++ b/risugen_loongarch64.pm
@@ -461,6 +461,7 @@ sub write_test_code($)
     my $condprob = $params->{ 'condprob' };
     my $fcsr = $params->{'fpscr'};
     my $numinsns = $params->{ 'numinsns' };
+    my $randseed = $params->{ 'randseed' };
     my $fp_enabled = $params->{ 'fp_enabled' };
     my $outfile = $params->{ 'outfile' };
 
@@ -473,8 +474,12 @@ sub write_test_code($)
     # probability of forcing insn to unconditional
     $condprob = 1 - $condprob;
 
-    # TODO better random number generator?
-    srand(0);
+    # use time() as random gen seed by default
+    if ($randseed == -1) {
+        srand(time());
+    } else {
+        srand($randseed);
+    }
 
     print "Generating code using patterns: @keys...\n";
     progress_start(78, $numinsns);
diff --git a/risugen_m68k.pm b/risugen_m68k.pm
index 7d62b13..85fc3da 100644
--- a/risugen_m68k.pm
+++ b/risugen_m68k.pm
@@ -158,6 +158,7 @@ sub write_test_code($)
 
     my $condprob = $params->{ 'condprob' };
     my $numinsns = $params->{ 'numinsns' };
+    my $randseed = $params->{ 'randseed' };
     my $outfile = $params->{ 'outfile' };
 
     my %insn_details = %{ $params->{ 'details' } };
@@ -172,8 +173,12 @@ sub write_test_code($)
     # probability of forcing insn to unconditional
     $condprob = 1 - $condprob;
 
-    # TODO better random number generator?
-    srand(0);
+    # use time() as random gen seed by default
+    if ($randseed == -1) {
+        srand(time());
+    } else {
+        srand($randseed);
+    }
 
     print "Generating code using patterns: @keys...\n";
     progress_start(78, $numinsns);
diff --git a/risugen_ppc64.pm b/risugen_ppc64.pm
index b241172..4bc2d62 100644
--- a/risugen_ppc64.pm
+++ b/risugen_ppc64.pm
@@ -368,9 +368,9 @@ sub write_test_code($)
 
     my $condprob = $params->{ 'condprob' };
     my $numinsns = $params->{ 'numinsns' };
+    my $randseed = $params->{ 'randseed' };
     my $fp_enabled = $params->{ 'fp_enabled' };
     my $outfile = $params->{ 'outfile' };
-
     my %insn_details = %{ $params->{ 'details' } };
     my @keys = @{ $params->{ 'keys' } };
 
@@ -384,8 +384,12 @@ sub write_test_code($)
     # probability of forcing insn to unconditional
     $condprob = 1 - $condprob;
 
-    # TODO better random number generator?
-    srand(0);
+    # use time() as random gen seed by default
+    if ($randseed == -1) {
+        srand(time());
+    } else {
+        srand($randseed);
+    }
 
     print "Generating code using patterns: @keys...\n";
     progress_start(78, $numinsns);
-- 
2.34.1

