optimise random string generate algorithm