Skip to content

Instantly share code, notes, and snippets.

@OkiStuff
Last active January 20, 2022 03:43
Show Gist options
  • Save OkiStuff/30d07b2fa00b0a31df0507411858beb1 to your computer and use it in GitHub Desktop.
Save OkiStuff/30d07b2fa00b0a31df0507411858beb1 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ASSET_TITLE_SIZE 256
#define MAX_ASSET_TABLE_SIZE 50
unsigned int __hash(const char *title)
{
int len = strnlen(title, MAX_ASSET_TITLE_SIZE);
unsigned int hash_val = 0;
for (int i = 0; i < len; i++)
{
hash_val = ((hash_val + (title[i] + 1)) * title[i]) % MAX_ASSET_TABLE_SIZE;
srand(hash_val);
hash_val = rand() % MAX_ASSET_TABLE_SIZE;
}
return hash_val;
}
int main()
{
int x = __hash("hola!!");
printf("%d\n", x);
x = __hash("hoblaaa!!");
printf("%d", x);
/**
Output:
39
21
**/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment