dimanche 3 juin 2018

Algorithm to map string to a random number in range [0,100]

Requirement

Sorry it sounds like an homework assignment but I need this feature for a dll I'm implementing in our project.

  1. I have an array of strings.
  2. Each string is of length 16 of random chars [0-9a-z]
  3. I want to map each string to a random number in range [0,100]
  4. String 'X' will always be mapped to number 'Y'

Attempt

for (string strLine; std::getline(filein, strLine);)
{
    int iSum = 0;
    for (const auto& c : strLine)
        iSum += static_cast<int>(c);

    int iRand = iSum % 101;

    using namespace std;;

    fileout << strLine << "\t" << iSum << "\t" << iRand << endl;
}

Problem

I run this on a 1000 random strings. The results are not uniform. For example, I see 23 times the random number 65 while I only see 1 time the random number 83.

This is not a surprise since my mapping function is embarrassing.

I tried looking at Pseudo-random number generation and kinda got lost.




Aucun commentaire:

Enregistrer un commentaire