Tetris

Check-in [aede015603]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Initial revision
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:aede0156036e8288b4960901a4185ddc499646cd37d3abc885a7b201af7d72b4
User & Date: ajv-899-334-8894@vsta.org 2008-10-26 17:41:53
Context
2008-10-26
17:42
Add random.c check-in: e130331ce3 user: ajv-899-334-8894@vsta.org tags: trunk
17:41
Initial revision check-in: aede015603 user: ajv-899-334-8894@vsta.org tags: trunk
2008-10-25
02:55
Add -Wall and cleanup rules check-in: 613a9e7d8d user: ajv-899-334-8894@vsta.org tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added random.c.





















































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * random.c
 *	Define interface to random numbers
 *
 * If we have a /dev/XXX interface into a random number generator,
 *  URANDOM is defined to that filename.  Otherwise we fall back on
 *  the usual locally initialized random number generator.
 */
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include "tetris.h"

#ifdef URANDOM

#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <strings.h>

static int rand_fd;

#else
#include <time.h>
#endif

/*
 * init_random()
 *	Set up to generate random numbers
 */
void
init_random(void)
{
#ifdef URANDOM
    rand_fd = open(URANDOM, O_RDONLY);
    if (rand_fd < 0) {
	perror(URANDOM);
	exit(1);
    }
#else
    srandom(getpid() ^ getppid() ^ time((void *)0));
#endif
}

/*
 * get_random()
 *	Get next random number from generator
 */
unsigned int
get_random(void)
{
#ifdef URANDOM
    static char rstate[1024 * sizeof(unsigned int)];
    static int pos = sizeof(rstate);
    unsigned int res;

    /* Refill from our random source when needed */
    if (pos >= sizeof(rstate)) {
	if (read(rand_fd, rstate, sizeof(rstate)) != sizeof(rstate)) {
	    perror(URANDOM);
	    exit(1);
	}
	pos = 0;
    }

    /* Get next random value and advance in buffer */
    bcopy(&rstate[pos], &res, sizeof(unsigned int));
    pos += sizeof(unsigned int);

    return(res);
#else
    return(random());
#endif
}