2013-04-18 74 views
3

我試圖隨機字符的出現在遊戲中,但用自己的名字作爲種子。所以,如果你以前遇到過「鮑勃」在比賽中,他永遠有相同的頭髮/眼睛等如何平滑隨機分佈?

現在我只是從生成的名稱中的數字(加入所有的字符代碼)和然後使用模數來決定他們擁有哪些選項。

實施例:Bob的種子是276(66 + 111 + 98)。 276%的髮型(40)的數量在36

這工作正常,但爲350+人的名單產生,分佈如下:

hair style: 0/# of people using it: 15 
hair style: 1/# of people using it: 8 
hair style: 2/# of people using it: 4 
hair style: 3/# of people using it: 5 
hair style: 4/# of people using it: 7 
hair style: 5/# of people using it: 5 
hair style: 6/# of people using it: 7 
hair style: 7/# of people using it: 14 
hair style: 8/# of people using it: 12 
hair style: 9/# of people using it: 6 
hair style: 10/# of people using it: 7 
hair style: 11/# of people using it: 2 
hair style: 12/# of people using it: 7 
hair style: 13/# of people using it: 10 
hair style: 14/# of people using it: 11 
hair style: 15/# of people using it: 7 
hair style: 16/# of people using it: 12 
hair style: 17/# of people using it: 7 
hair style: 18/# of people using it: 6 
hair style: 19/# of people using it: 10 
hair style: 20/# of people using it: 5 
hair style: 21/# of people using it: 10 
hair style: 22/# of people using it: 11 
hair style: 23/# of people using it: 3 
hair style: 24/# of people using it: 6 
hair style: 25/# of people using it: 8 
hair style: 26/# of people using it: 5 
hair style: 27/# of people using it: 11 
hair style: 28/# of people using it: 10 
hair style: 29/# of people using it: 6 
hair style: 30/# of people using it: 13 
hair style: 31/# of people using it: 11 
hair style: 32/# of people using it: 10 
hair style: 33/# of people using it: 12 
hair style: 34/# of people using it: 3 
hair style: 35/# of people using it: 11 
hair style: 36/# of people using it: 9 
hair style: 37/# of people using it: 4 
hair style: 38/# of people using it: 10 
hair style: 39/# of people using it: 15 

的分佈不是很順暢,它遍佈全球(不出所料)。我會碰到很多髮型#0的人,並且沒有髮型#11的人。

我怎樣才能順利完成這一出位?

+0

你能補充頭髮樣式打造的髮型數量是素數? –

+0

你必須讓同名的人擁有相同的髮型嗎?或者它是靈活的,只要你能找到一種方法來平滑分配? – Calpis

回答

4

,如果你使用真正的哈希函數,而不是僅僅求和ASCII/Unicode碼點你可能有更好的運氣。 The djb2 function頗爲流行的ASCII輸入:

unsigned long hash(unsigned char *str) { 
    unsigned long hash = 5381; 
    int c; 

    while (c = *str++) 
     hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 

    return hash; 
} 

因此,例如,

unsigned long hairStyle = hash(characterName) % kHairStyleCount; 

參見What's a good hash function for English words?

+0

+1:我同意這將是值得使用真正的散列函數。 – Simon

+0

也值得嘗試使用哈希作爲一個java.util.Random的對象的種子,並生成你需要的方式,而不是通過簡單的模塊化算術整數。在短名稱的數據中,您很可能會看到很多「怪異模式」,否則。 – torquestomp