算法随机字符 [英] Algorithm for random characters

查看:133
本文介绍了算法随机字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我弄糊涂两种算法生成随机字符之间。我有四个字符类型,小写字母,大写字母,数字和特殊字符。根据随机字符请求的类型,它应产生的字符。例如:

  • 如果请求的是纯粹的小写字母,大写字母,数字或特殊字符,则该值应随机从每个单独类型的选择。
  • 如果请求的是小写字母和数字,应该从两种类型的混合串选择。

我采用混合字符类型的算法如下:

  1. 选择每种类型的字符长度要求的随机字符串。
  2. Concat的这些字符串,然后选择新的字符串再次随机字符。

我认为,这个算法确保等于概率是在样本空间中可用的每个类型。然而,在某些情况下,混合类型的可将所得串具有无人psented在串和在一​​些情况下,许多我们可能会得到一些重复的字符的类型$ P ​​$的

还有另一种算法取决于$ P $对 - 混合的类型之前选择任意的字符串,但是,由于没有一种类型是更可能发生,但重复被降低。

我需要知道是否有另一种算法,提供的每一种类型的presentation,减少重复的平衡?

以下是实现我提到,首先,上述算法的code。这是CakePHP的1.2.x版本组件和工作的例子可能是这里发现:

 < PHP
类RandomStringComponent扩展对象{
  变量$名称='RandomString';
  变量$小写='qwertyuiopasdfghjklzxcvbnm';
  变量$大写='ASDFGHJKLQWERTYUIOPZXCVBNM';
  VAR $编号='9874563210';
  变量$ specialChars中='@#$%) -  + /}<>〜=;:|`{?]!^&放大器; *(';
  变量$ outputForm ='LcUcNuSp';
  变量$设置=阵列();
  变量$控制器= NULL;

  变量$ _defaults =阵列(
      outputForm'=> LcUcNuSp,
      长度=> 6
  );

  函数初始化(安培; $控制器,$设置){
    $这个 - >设置= array_merge($这个 - > _defaults,$设置);

  }

  功能getRand($ L = NULL){
    如果(is_null($ 1))$ L = $这个 - >设置['长'];
    开关($这个 - >设置['outputForm']){
      案LC:
        返回$这个 - > _Lc($ L);
        打破;
      案Uc的:
        返回$这个 - > _Uc($ L);
        打破;
      案SP:
        返回$这个 - > _SP($ L);
        打破;
      案女:
        返回$这个 - > _Nu($ L);
        打破;
      案LcUc:
        返回$这个 - > _setRand($这个 - > _Lc($ L)$这个 - > _Uc($ L),$ L);
        打破;
      案LcNu:
        返回$这个 - > _setRand($这个 - > _Lc($ L)$这个 - > _Nu($ L),$ L);
        打破;
      案UcNu:
        返回$这个 - > _setRand($这个 - > _Uc($ L)$这个 - > _Nu($ L),$ L);
        打破;
      案LcUcNu:
        返回$这个 - > _setRand($这个 - > _Lc($ L)$这个 - > _Uc($ L)$这个 - > _Nu($ L),$ L);
        打破;
      默认:
        返回$这个 - > _setRand($这个 - > _Lc($ L)$这个 - > _Uc($ L)$这个 - > _Nu($ L)$这个 - > _SP($ L ),$升);
        打破;
    }
  }


  功能_Lc($ L){
    $输出='';
    为($ i = 0; $ I< $ L; $ I ++){
      $输出= $这个 - >小写字母[mt_rand(0,strlen的($这个 - >小写)-1);
    }
    返回$输出;
  }
  功能_Uc($ L){
    $输出='';
    为($ i = 0; $ I< $ L; $ I ++){
      $输出= $这个 - >大写的[mt_rand(0,strlen的($这个 - >大写)-1);
    }
    返回$输出;
  }
  功能_SP($ L){
    $输出='';
    为($ i = 0; $ I< $ L; $ I ++){
      $输出= $这个 - > specialChars中[mt_rand(0,strlen的($这个 - > specialChars中)-1);
    }
    返回$输出;
  }
  功能_Nu($ L){
    $输出='';
    为($ i = 0; $ I< $ L; $ I ++){
      $输出= $这个 - >数字[mt_rand(0,strlen的($这个 - >数字)-1);
    }
    返回$输出;
  }

  功能_setRand($海峡,$ L){

    $输出='';
    为($ i = 0; $ I< $ L; $ I ++){
      $输出= $海峡[mt_rand(0,strlen的($海峡)-1)。
    }
    返回$输出;
  }
}
?>
 

解决方案

您可以消除重复抽样无需更换。如果你想一些重复,重复使用的某个k值的样品,其追加到原来的列表中,样品无需更换。

例如:小写='qwertyuiopasdfghjklzxcvbnm

sampledLowercase =采样从小写5个字母='abbcd

他们从小写+ sampledLowercase样无需更换。

下面找到一个重复字符的概率是大约5 /(26 + 5)= 5/31。 sampledLowercase的长度更长,更高,将有重复的字母的概率。

I get confused between two algorithms for generating random characters. I have four characters types, lowercase, uppercase, numbers and special characters. According to the type of random characters requested, it should produce characters. For example:

  • If the requested is pure lowercase, uppercase, numbers or special characters, the value should be chosen randomly from each type alone.
  • If the requested is lowercase and numbers it should be chosen from a mixed string of that two types.
  • etc

The algorithm I adopt for mixed characters type is as the following:

  1. Choose random string from each type with the characters length requested.
  2. Concat those strings and then choose again random characters from the new string.

I think that this algorithm assure equal probabilities for each type to be available in the sample space. However, in some cases the resultant string of mixed types may has no one of the type presented in the string and In some many cases we may get some repeated characters.

There is another algorithm depends on pre-mixing the types before choose the random string, However, the absence of one type is more likely occurred but the repetition is reduced.

I need to know if there is another algorithm that offers balance between presentation of each type and reduce repetition?

The following is the code that implement the algorithm that I mention, firstly, above. It is CakePHP 1.2.x component and a working example could be found here:

<?php
class RandomStringComponent extends Object{
  var $name = 'RandomString';
  var $lowerCase = 'qwertyuiopasdfghjklzxcvbnm';
  var $upperCase = 'ASDFGHJKLQWERTYUIOPZXCVBNM';
  var $numbers = '9874563210';
  var $specialChars = '!@#$%)-+/}[<>~=,;:|`{.?]^&*(';
  var $outputForm = 'LcUcNuSp';
  var $settings = array();
  var $controller = null;      

  var $_defaults = array(
      'outputForm' => 'LcUcNuSp',
      'length' => 6
  );

  function initialize(&$controller, $settings){
    $this->settings = array_merge($this->_defaults, $settings);

  }

  function getRand($l = null){
    if (is_null($l)) $l = $this->settings['length'];
    switch ($this->settings['outputForm']){
      case 'Lc':
        return $this->_Lc($l);
        break;
      case 'Uc':
        return $this->_Uc($l);
        break;
      case 'Sp':
        return $this->_Sp($l);
        break;
      case 'Nu':
        return $this->_Nu($l);
        break;
      case 'LcUc':
        return $this->_setRand($this->_Lc($l).$this->_Uc($l),$l);
        break;
      case 'LcNu':
        return $this->_setRand($this->_Lc($l).$this->_Nu($l),$l);
        break;
      case 'UcNu':
        return $this->_setRand($this->_Uc($l).$this->_Nu($l),$l);
        break;
      case 'LcUcNu':
        return $this->_setRand($this->_Lc($l).$this->_Uc($l).$this->_Nu($l),$l);
        break;
      default:
        return $this->_setRand($this->_Lc($l).$this->_Uc($l).$this->_Nu($l).$this->_Sp($l),$l);
        break;
    }
  }


  function _Lc($l){
    $output = '';
    for ($i = 0; $i < $l; $i++){      
      $output .= $this->lowerCase[mt_rand(0, strlen($this->lowerCase)-1)];    
    }
    return $output;
  }
  function _Uc($l){
    $output = '';
    for ($i = 0; $i < $l; $i++){
      $output .= $this->upperCase[mt_rand(0, strlen($this->upperCase)-1)];    
    }
    return $output;
  }
  function _Sp($l){
    $output = '';
    for ($i = 0; $i < $l; $i++){
      $output .= $this->specialChars[mt_rand(0, strlen($this->specialChars)-1)];      
    }
    return $output;
  }
  function _Nu($l){
    $output = '';
    for ($i = 0; $i < $l; $i++){
      $output .= $this->numbers[mt_rand(0, strlen($this->numbers)-1)];    
    }
    return $output;
  }

  function _setRand($str, $l){

    $output = '';
    for ($i = 0; $i < $l; $i++){
      $output .= $str[mt_rand(0, strlen($str)-1)];    
    }
    return $output; 
  }  
}
?>

解决方案

You can eliminate repetition by sampling without replacement. If you want "some" repetition, you sample with repetition for some k values, append it to the original list, and sample without replacement.

Eg: lowerCase = 'qwertyuiopasdfghjklzxcvbnm'

sampledLowercase = Sample 5 letters from lowercase = 'abbcd'

Them sample from lowercase+sampledLowercase without replacement.

Here the probability of finding a repeated character is about 5/(26+5) = 5/31. The longer the length of sampledLowercase, higher the probability that there will be duplicate letters.

这篇关于算法随机字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆