vendredi 6 novembre 2015

Language-independent random number generator from alpha-numeric string

I'm writing a program where I want to be able to generate a random integer within a set range from a seed of two 10-character alpha-numeric strings (for example: "bGmwcRG6ch" and "7AdFJ4VyY3"). I also want the same number to be generated no matter what language I am running this on. This number will correspond to a predefined username stored within the program.

The part I'm having the most trouble with is I want to be able to add more usernames (increase the range of the set) in the future without changing everyone's usernames. Currently I have 200.

What I have so far (written in Java):

public String generateUsername(String userId, String chatId)
{
    String s = userId + chatId;

    long seed = 0;

    for (int i = 0; i < s.length(); i++)
    {
        seed = seed + s.charAt(i);
    }

    Log.d("chatId", chatId);
    Log.d("userId", userId);
    Log.d("Seed: ", Long.toString(seed));        
}

Log output:

11-07 14:58:45.902 10666-10666/? D/chatId: bGmwcRG6ch

11-07 14:58:45.902 10666-10666/? D/userId: 7AdFJ4VyY3

11-07 14:58:45.902 10666-10666/? D/Seed:: 1669

Any advice would be much appreciated.




Aucun commentaire:

Enregistrer un commentaire