cnumpop

numpop in c
git clone git://tx.god.jp/cnumpop.git
Log | Files | Refs | README

commit a0bc290931267ca1b7eb562740f6312ef7bb3208
parent bc0f32b88e83708755a843ef5f687b18fe937fe7
Author: tx <trqx@goat.si>
Date:   Fri Apr 28 10:36:03 +0200

use djb2 hash function

Diffstat:
cnumpop.c | 24+++++++++++++-----------
1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/cnumpop.c b/cnumpop.c @@ -91,24 +91,26 @@ void gamerun(struct Game *game) { printf("final score: %d\n", game->score); } +// djb2 +unsigned long hash(unsigned char *str) { + unsigned long hash = 5381; + int c; -// pseudo hash, make int out of string -int inthash (const char* word) -{ - unsigned int hash = 0; - for (int i = 0 ; word[i] != '\0' ; i++) - { - hash = 31*hash + word[i]; - } - return hash % 666; + while ((c = *str++)) + hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + + return hash; } + int main(int argc, char *argv[]) { + int seed; if (argc == 2) { - srand(inthash(argv[1])); + seed = hash(argv[1]); } else { - srand((int)time(NULL)); + seed = (int)time(NULL); } + srand(seed); struct Game game; initgame(&game);