给定一个字符串,我们的任务是使用字符,特殊字符,数字等的随机组合生成一些字符串。
Input PP Output AK AK . . . . .
Step 1: Input a string. Step2: Here we store all possible combination of lowercase, uppercase and special characters in a variable. Step3: Use two loops and use random function. From this, we can get all the possible combinations of characters, symbols. Step4: At the end display same string which is same as an input string and it matches each random string with given input string. Step5: If both index values are same then store the index and iterate for the remaining.
import string import random import time my_possibleCharacters = string.ascii_lowercase + string.digits + string.ascii_uppercase + ' ., !?;:' t = "ab" my_attemptThis = ''.join(random.choice(my_possibleCharacters) for i in range(len(t))) my_attemptNext = '' com = False iteration = 0 # Iterate while completed is false while com == False: print(my_attemptThis) my_attemptNext = '' com = True for i in range(len(t)): if my_attemptThis[i] != t[i]: com = False my_attemptNext += random.choice(my_possibleCharacters) else: my_attemptNext += t[i] # increment the iteration iteration += 1 my_attemptThis = my_attemptNext time.sleep(0.1) # Driver Code print("String matched after " + str(iteration) + " iterations")
输出结果
36 G sM ,L jt g1 FN uR ;W Ja 3n 4o Gl kY NR oR Nw Lg Jt Od wN z0 J 3a 9J sF v g6 HO Ia AB Xa OX :N Wo Dp f; tt kf Er In ou bD T a0 aH aW a a8 ai ax az aN aJ ah a0 a. aq ar ax ai am a; aO as a; aS aL aQ a8 a3 ae a5 aS ao al aV ar aj aT aS ad ab String matched after 83 iterations
本文向大家介绍Python生成给定长度的随机字符串,包括了Python生成给定长度的随机字符串的使用技巧和注意事项,需要的朋友参考一下 在本文中,我们将看到如何生成具有给定长度的随机字符串。这在创建需要随机性的随机密码或其他程序时很有用。 random.choices 随机模块中的choices函数可以产生字符串,然后可以将其连接以创建给定长度的字符串。 示例 输出结果 运行上面的代码给我们以下结
本文向大家介绍PHP 生成随机字符串,包括了PHP 生成随机字符串的使用技巧和注意事项,需要的朋友参考一下 要使用PHP生成随机字符串,代码如下- 示例 输出结果 示例 现在让我们来看另一个示例- 输出结果
cmf_random_string($len = 6) 功能 随机字符串生成 参数 $len: string 生成的字符串长度 返回 string 随机字符串
本文向大家介绍用Java生成随机字符串,包括了用Java生成随机字符串的使用技巧和注意事项,需要的朋友参考一下 让我们首先声明一个字符串数组并初始化- 现在,创建一个Random对象- 生成随机字符串- 示例 输出结果 让我们再次运行它以获得不同的随机字符串-
问题内容: 我正在尝试在PHP中创建一个随机字符串,并且我对此绝对没有输出: 我究竟做错了什么? 问题答案: 要具体回答这个问题,有两个问题: 当您回显它时,它不在范围内。 这些字符在循环中没有并置在一起。 这是包含更正的代码段: 通过以下调用输出随机字符串:
<?php $random=sp_random_string();//不指定位数,默认为6位 echo $random; //或者 $random=sp_random_string(8);//指定返回8位随机字符串 echo $random; ?>