Generate random
#3

Or you can just not fail and do something similar to this;

pawn Код:
GenerateRandomString()
{
    new str[24];
   
    // 0 = char 48
    // A = char 65
    // a = char 97
   
    for(new i; i < 20; i++) // 20 total chars
    {
        if(i % 4 == 0) // If 'i' is a multiple of 4
        {
            str[i] = '-'; // Insert dash
        }
        else
        {
            switch(random(36)) // 36 cuz 10+26, or 10 digits and 26 alphabet chars
            {
                case 0..9:
                    str[i] = 48 + random(10); // random of 10 digits
                default:
                    str[i] = 65 + random(26); // 65 (char 'A') + random(26), cuz 26 letters in alphabet
            }
        }
    }
   
    return str;
}


@TokicMajstor: Please don't go around "helping" if you don't know how... Generally your answer is okay, but knowing that it probably won't work and saying "come back if you have errors" isn't going to help anyone. This is the section new users are supposed to learn from, not a place for inexperienced users to post things that aren't going to teach someone anything. FOR EXAMPLES; "random(9)" is only going to be 0-8, excluding number 9. "random(RandomLetters[5]);" is going to return an error because "RandomLetters[5]" doesn't represent an integer (which is the only parameter the random function takes), instead you are going to get an out of range error and a confused compiler. The ONLY thing you got right was the number 20 for the "code" array size, and even that is wrong because a null character is needed (requiring the size to be 21).
Reply


Messages In This Thread
Generate random - by SymonClash - 28.03.2019, 20:44
Re: Generate random - by TokicMajstor - 28.03.2019, 23:11
Re: Generate random - by Crayder - 29.03.2019, 03:07
Re: Generate random - by TheToretto - 29.03.2019, 08:22
Re: Generate random - by SymonClash - 29.03.2019, 08:24
Re: Generate random - by bgedition - 29.03.2019, 16:45
Re: Generate random - by Crayder - 30.03.2019, 06:50
Re: Generate random - by Nero_3D - 30.03.2019, 12:00
Re: Generate random - by Crayder - 30.03.2019, 16:31

Forum Jump:


Users browsing this thread: 1 Guest(s)