Thread Random generator (24 answers)
Opened by frodus at 2006-03-23 12:01

esskar
 2006-03-23 22:20
#64027 #64027
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
so, jetzt müsstest du genug zahlen erzeugen können, ohne eine kollision zu bekommen

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/perl

use strict;
use warnings;

use Digest ();
use Math::BigInt ();

sub digest_mix {
   my ($random1) = @_;

   my $ctx = Digest->new("SHA-256");
   for my $i (0 .. 31) {
       my $r = $random1->copy;
       my $c = $r->band(0xff);
       $ctx->add($c);
       $random1->brsft(8);
   }

   return $ctx->hexdigest;
}

{
   my $_rand = Math::BigInt->new("0x" . unpack("H*", pack("L", time)));
   sub srandom {
       $_rand = Math::BigInt->new(shift) if @_;
       return $_rand;
   }

   sub random {
       $_rand = Math::BigInt->new("0x" . digest_mix($_rand));
       return $_rand->bstr;
   }
}
\n\n

<!--EDIT|esskar|1143145482-->

View full thread Random generator