L
L
lexstile2020-09-01 18:28:31
PHP
lexstile, 2020-09-01 18:28:31

How to write an algorithm in php to generate a controlled random string?

Need to control:

  • the total number of characters in a random string;
  • the number of certain characters in a random string (for example: so that the character "Y" occurs only 13 times);
  • transferred characters.

That is, it should be something like:
function custom_rand($length, $string, $symbol) {}
custom_rand(100, 'TY', 'Y'); // должна выйти строка длиной 100 символов, в которой 'Y' встречается 13 раз.
// 13 контролировать необязательно

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
scottparker, 2020-09-01
@lexstile

countLetter = 0
result = ''
в цикле от 0 до length 
  index = получаешь случайное число от 0 до длины string
  letter = берешь букву с индексом index из string
  добавляешь letter к result 
  если letter  == symbol{
    countLetter++
    если countLetter == 13{
      удаляешь symbol из string
    }
  }
возвращаешь result

something like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question