1 /* random.c - Z-machine random number generator
2 * Copyright (c) 1995-1997 Stefan Jokisch
4 * This file is part of Frotz.
6 * Frotz is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Frotz is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 static int interval = 0;
26 static int counter = 0;
31 * Set the seed value for the random number generator.
35 void seed_random (int value)
38 if (value == 0) { /* ask interface for seed value */
39 A = os_random_seed ();
41 } else if (value < 1000) { /* special seed value */
44 } else { /* standard seed value */
52 * z_random, store a random number or set the random number seed.
54 * zargs[0] = range (positive) or seed value (negative)
61 if ((short) zargs[0] <= 0) { /* set random seed */
63 seed_random (- (short) zargs[0]);
66 } else { /* generate random number */
70 if (interval != 0) { /* ...in special mode */
72 if (counter == interval) counter = 0;
73 } else { /* ...in standard mode */
74 A = 0x015a4e35L * A + 1;
75 result = (A >> 16) & 0x7fff;
78 store ((zword) (result % zargs[0] + 1));