cachesim
A cache simulator
random.c
Go to the documentation of this file.
00001 /* -*- mode:c; coding: utf-8 -*- */
00002 
00003 #include "random.h"
00004 #include "common.h"
00005 
00006 #include <stdlib.h>
00007 #include <time.h>
00008 
00013 struct Random
00014 {
00015     RandomOps *ops; 
00016     int seed; 
00017 };
00018 
00019 Random *
00020 random_free(Random *rnd)
00021 {
00022     if (rnd) {
00023         free(rnd);
00024     }
00025     return NULL;
00026 }
00027 
00028 int
00029 random_next(Random *rnd, int n)
00030 {
00031     return (int)(rand() / (RAND_MAX + 1.0) * n);
00032 }
00033 
00034 static RandomOps random_ops =
00035 {
00036     random_free,
00037     random_next,
00038 };
00039 
00040 Random *
00041 random_create(ConfigFile *cfg)
00042 {
00043     char buf[1024];
00044     Random *rnd = (Random*) calloc(1, sizeof(*rnd));
00045     rnd->ops = &random_ops;
00046 
00047     int r = config_file_get_int(cfg, "seed", &rnd->seed);
00048     if (!r) {
00049         //error_undefined("random_create", buf);
00050         rnd->seed = time(NULL);
00051     } else if (r < 0 || rnd->seed <= 0) {
00052         error_invalid("random_create", buf);
00053     }
00054 
00055     return rnd;
00056 }
00057 
00058 /*
00059  * Local variables:
00060  *  c-basic-offset: 4
00061  * End:
00062  */