marabunta
Goto Top

Zufälliges Wort muss mindestens einige definierte Buchstaben enthalten

Hallo,

ich habe eine kleine Herausforderung, dass ein Zufällig erstelltes Wort bestimmte Zeichen mindestens einmal enthalten muss.
Bisher kann ich ein Wort erstellen und eines der bestimmten Zeichen zufällig einsetzen,
ohne sicherzustellen, dass am ende jedes der bestimmten Zeichen mindestens einmal vorgekommen ist.
Bzw. es muss so oft vorkommen wie es in $fixcharacter drinsteht.
Bspw. 2x f bei $fixcharacter bedeutet mindestens 2xf im finalen Wort.
Hat da jemand eine Idee?

$wort="123456789abcdefghijklmno" #dummy  

for($n=0;$n -lt 100;$n++){ #100 random words

$m=0
$word=""  

$fixcharacter="irfsfM" #each character at least once  
$numberfixcharacter=$fixcharacter.Length #get number of fixed characters
$positions=Get-Random -InputObject 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 -Count $numberfixcharacter #generate random position for fixcharacter
#$positions

for($m=0;$m -lt 25; $m++){
    if(!$positions.Contains($m)){ #if position is one of the generated random positions, insert random letter of fixcharacter
        $character=-join ((65..90) | Get-Random -Count 1 | % {[char]$_})
        $word+=$character
    }else{
       $word+=Get-Random -Inputobject $fixcharacter.ToCharArray()
    }
}
$word
}

Danke

Content-ID: 1594364591

Url: https://administrator.de/contentid/1594364591

Printed on: December 2, 2024 at 17:12 o'clock

Marabunta
Solution Marabunta Dec 06, 2021 at 23:20:18 (UTC)
Goto Top
Ist zwar aufwendig, aber funktioniert.
$wort="123456789abcdefghijklmno"  

$a_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'a'} | Measure-Object).Count  
$b_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'b'} | Measure-Object).Count  
$c_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'c'} | Measure-Object).Count  
$d_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'd'} | Measure-Object).Count  
$e_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'e'} | Measure-Object).Count  
$f_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'f'} | Measure-Object).Count  
$g_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'g'} | Measure-Object).Count  
$h_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'h'} | Measure-Object).Count  
$i_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'i'} | Measure-Object).Count  
$j_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'j'} | Measure-Object).Count  
$k_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'k'} | Measure-Object).Count  
$l_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'l'} | Measure-Object).Count  
$m_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'm'} | Measure-Object).Count  
$n_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'n'} | Measure-Object).Count  
$o_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'o'} | Measure-Object).Count  
$p_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'p'} | Measure-Object).Count  
$q_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'q'} | Measure-Object).Count  
$r_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'r'} | Measure-Object).Count  
$s_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 's'} | Measure-Object).Count  
$t_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 't'} | Measure-Object).Count  
$u_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'u'} | Measure-Object).Count  
$v_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'v'} | Measure-Object).Count  
$w_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'w'} | Measure-Object).Count  
$x_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'x'} | Measure-Object).Count  
$y_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'y'} | Measure-Object).Count  
$z_number=($fixcharacter.tochararray()| Where-Object {$_ -eq 'z'} | Measure-Object).Count  


for($n=0;$n -lt 100;$n++){ #100 words

$m=0
$word=""  

$fixcharacter="irfsfM" #each character at least once  
$numberfixcharacter=$fixcharacter.Length #get number of fixed characters
$positions=Get-Random -InputObject 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 -Count $numberfixcharacter
#$positions


for($m=0;$m -lt 25; $m++){
    if(!$positions.Contains($m)){
        $character=-join ((65..90) | Get-Random -Count 1 | % {[char]$_})
        $word+=$character
    }else{
       $word+=Get-Random -Inputobject $fixcharacter.ToCharArray()
    }
}


#prüfung
$wordvalid=$true


$a2_number=($word.tochararray()| Where-Object {$_ -eq 'a'} | Measure-Object).Count  
$b2_number=($word.tochararray()| Where-Object {$_ -eq 'b'} | Measure-Object).Count  
$c2_number=($word.tochararray()| Where-Object {$_ -eq 'c'} | Measure-Object).Count  
$d2_number=($word.tochararray()| Where-Object {$_ -eq 'd'} | Measure-Object).Count  
$e2_number=($word.tochararray()| Where-Object {$_ -eq 'e'} | Measure-Object).Count  
$f2_number=($word.tochararray()| Where-Object {$_ -eq 'f'} | Measure-Object).Count  
$g2_number=($word.tochararray()| Where-Object {$_ -eq 'g'} | Measure-Object).Count  
$h2_number=($word.tochararray()| Where-Object {$_ -eq 'h'} | Measure-Object).Count  
$i2_number=($word.tochararray()| Where-Object {$_ -eq 'i'} | Measure-Object).Count  
$j2_number=($word.tochararray()| Where-Object {$_ -eq 'j'} | Measure-Object).Count  
$k2_number=($word.tochararray()| Where-Object {$_ -eq 'k'} | Measure-Object).Count  
$l2_number=($word.tochararray()| Where-Object {$_ -eq 'l'} | Measure-Object).Count  
$m2_number=($word.tochararray()| Where-Object {$_ -eq 'm'} | Measure-Object).Count  
$n2_number=($word.tochararray()| Where-Object {$_ -eq 'n'} | Measure-Object).Count  
$o2_number=($word.tochararray()| Where-Object {$_ -eq 'o'} | Measure-Object).Count  
$p2_number=($word.tochararray()| Where-Object {$_ -eq 'p'} | Measure-Object).Count  
$q2_number=($word.tochararray()| Where-Object {$_ -eq 'q'} | Measure-Object).Count  
$r2_number=($word.tochararray()| Where-Object {$_ -eq 'r'} | Measure-Object).Count  
$s2_number=($word.tochararray()| Where-Object {$_ -eq 's'} | Measure-Object).Count  
$t2_number=($word.tochararray()| Where-Object {$_ -eq 't'} | Measure-Object).Count  
$u2_number=($word.tochararray()| Where-Object {$_ -eq 'u'} | Measure-Object).Count  
$v2_number=($word.tochararray()| Where-Object {$_ -eq 'v'} | Measure-Object).Count  
$w2_number=($word.tochararray()| Where-Object {$_ -eq 'w'} | Measure-Object).Count  
$x2_number=($word.tochararray()| Where-Object {$_ -eq 'x'} | Measure-Object).Count  
$y2_number=($word.tochararray()| Where-Object {$_ -eq 'y'} | Measure-Object).Count  
$z2_number=($word.tochararray()| Where-Object {$_ -eq 'z'} | Measure-Object).Count  

if($a_number -gt $a2_number){$wordvalid=$false}
if($b_number -gt $b2_number){$wordvalid=$false}
if($c_number -gt $c2_number){$wordvalid=$false}
if($d_number -gt $d2_number){$wordvalid=$false}
if($e_number -gt $e2_number){$wordvalid=$false}
if($f_number -gt $f2_number){$wordvalid=$false}
if($g_number -gt $g2_number){$wordvalid=$false}
if($h_number -gt $h2_number){$wordvalid=$false}
if($i_number -gt $i2_number){$wordvalid=$false}
if($j_number -gt $j2_number){$wordvalid=$false}
if($k_number -gt $k2_number){$wordvalid=$false}
if($l_number -gt $l2_number){$wordvalid=$false}
if($m_number -gt $m2_number){$wordvalid=$false}
if($n_number -gt $n2_number){$wordvalid=$false}
if($o_number -gt $o2_number){$wordvalid=$false}
if($p_number -gt $p2_number){$wordvalid=$false}
if($q_number -gt $q2_number){$wordvalid=$false}
if($r_number -gt $r2_number){$wordvalid=$false}
if($s_number -gt $s2_number){$wordvalid=$false}
if($t_number -gt $t2_number){$wordvalid=$false}
if($u_number -gt $u2_number){$wordvalid=$false}
if($v_number -gt $v2_number){$wordvalid=$false}
if($w_number -gt $w2_number){$wordvalid=$false}
if($x_number -gt $x2_number){$wordvalid=$false}
if($y_number -gt $y2_number){$wordvalid=$false}
if($z_number -gt $z2_number){$wordvalid=$false}

if($true -eq $wordvalid){
    $word
}else{
    $n=$n-1
}
}
149569
Solution 149569 Dec 07, 2021 updated at 09:57:46 (UTC)
Goto Top
Kürzer gehts so

$passlength = 100
$chars = 48..57 + 65..90 + 97..122 | %{[char]$_}
$fixed = 'irfsfM'.ToCharArray()  
$final = (((1..($passlength-$fixed.Count)) | %{$chars | Get-Random -Count 1}) + $fixed | Get-Random -Count $passlength) -join ''  
$final
haichen
Solution haichen Dec 07, 2021 updated at 11:14:01 (UTC)
Goto Top
Vielleicht auch so:
$fixcharacter="irfsfM"  
$wordlength=25
$wordcount=100
$partlength=$wordlength-$fixcharacter.Length

for($n=0;$n -lt $wordcount;$n++){
	$word=""  
	for($m=0;$m -lt $partlength; $m++){
		$character=-join ((65..90) | Get-Random -Count 1 | % {[char]$_})
		$word+=$character
	}
	$word+=$fixcharacter
	$word = -join ($word.ToCharArray() | Sort-Object {Get-Random}) 
	$word
}

..
EHfZJDrKLUYsFiMRQGQXPBPFf
KIKOVNKiYfPVMrNMJFsfVIAMN
WIrNAiIKWYDGCfHVMQsUNCVNf
LiCKWBIQAMAOLIffGKAWOrOKs
ZRVTOCAHErIZNTSMKfNYifsWD
VfFsMiCUTPEYHJKfXRRCDRXLr
..

Ich war nicht schnell genug.. face-smile