egra: moved "disabled" and "accept/cancel" properties to the base widget class
[iv.d.git] / prng / seeder.d
blobb0421237a9d6d6d633f3e5e630a4f0c30f405610
1 /* Invisible Vector Library
2 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
3 * Understanding is not required. Only obedience.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 module iv.prng.seeder /*is aliced*/;
18 import iv.alice;
21 public uint xyzzyPRNGHashU32() (uint a) {
22 a -= (a<<6);
23 a ^= (a>>17);
24 a -= (a<<9);
25 a ^= (a<<4);
26 a -= (a<<3);
27 a ^= (a<<10);
28 a ^= (a>>15);
29 return a;
33 public uint getTwoUintSeeds (uint* second=null) nothrow @trusted @nogc {
34 version(Windows) {
35 import core.sys.windows.windef, core.sys.windows.winbase;
36 uint s0 = xyzzyPRNGHashU32(cast(uint)GetCurrentProcessId());
37 uint s1 = xyzzyPRNGHashU32(cast(uint)GetTickCount());
38 if (second is null) return s0^s1;
39 *second = s1;
40 return s0;
41 } else {
42 // assume POSIX
43 import core.sys.posix.fcntl;
44 import core.sys.posix.unistd;
45 uint s0 = 0xdeadf00du;
46 int fd = open("/dev/urandom", O_RDONLY);
47 if (fd >= 0) {
48 // assume that we always have endless supply of bytes from that
49 read(fd, &s0, s0.sizeof);
50 if (second !is null) read(fd, second, (*second).sizeof);
51 close(fd);
52 return s0;
54 // try something another
55 import core.sys.posix.unistd;
56 import core.sys.posix.time;
57 s0 = cast(uint)xyzzyPRNGHashU32(cast(uint)getpid());
58 timespec stt = void;
59 if (clock_gettime(CLOCK_MONOTONIC, &stt) == 0) {
60 uint s1 = xyzzyPRNGHashU32(cast(uint)(stt.tv_sec^stt.tv_nsec));
61 if (second is null) return s0^s1;
62 *second = s1;
63 return s0;
65 if (second is null) *second = xyzzyPRNGHashU32(s0);
66 return s0;
71 public ulong getUlongSeed () nothrow @trusted @nogc {
72 uint s1;
73 uint s0 = getTwoUintSeeds(&s1);
74 return ((cast(ulong)s1)<<32)|s0;