def getRandom(self): string = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" res = "" for i in range(16): res += string[int(random.random()*62)] return res
时间: 2024-04-28 09:19:56 浏览: 85
这是一个 Python 类中的一个方法,名为 getRandom。它的作用是生成一个长度为 16 的随机字符串,由数字和大小写字母组成。具体实现方式是先定义一个包含所有可能字符的字符串 string,然后通过循环生成一个长度为 16 的字符串 res,每次从 string 中随机选取一个字符加入 res 中。其中 random.random() 返回一个 0~1 之间的随机小数,乘以 62(string 的长度)后取整可以得到 0~61 之间的整数,用来从 string 中选取字符。最后返回生成的随机字符串 res。
相关问题
把这段java代码转换成python代码,public class Demo { public static void main(String[] args) { String random = getRandom(7); System.out.println("交换前:"+ random); Integer max = 0; char[] charsA = random.toCharArray(); char[] charsB = random.toCharArray(); for (int i = 0; i < random.length(); i++) { char temp = charsA[i]; if(temp == 0){ continue; } for (int j = i+1; j < random.length(); j++) { if(temp == charsA[j]){ continue; } charsB[i] = charsA[j]; charsB[j] = temp; String tempNum = new String(charsB); Integer newNum = Integer.valueOf(tempNum); if (max.compareTo(newNum) < 0) { max = newNum; } //还原比较对象 charsB = random.toCharArray(); //System.out.println("有效交换次:"+newNum); } } System.out.println("交换后:" + max); } public static String getRandom(int len) { int digit = (int) Math.pow(10, len - 1); int rs = new Random().nextInt(digit * 10); if (rs < digit) { rs += digit; } return String.valueOf(rs); } }
以下是该 Java 代码的 Python 实现:
import random
def get_random(len):
digit = 10 ** (len - 1)
rs = random.randint(digit, digit * 10 - 1)
return str(rs)
random = get_random(7)
print("交换前:" + random)
max_num = 0
charsA = list(random)
charsB = list(random)
for i in range(len(random)):
temp = charsA[i]
if temp == '0':
continue
for j in range(i + 1, len(random)):
if temp == charsA[j]:
continue
charsB[i], charsB[j] = charsA[j], temp
temp_num = ''.join(charsB)
new_num = int(temp_num)
if max_num < new_num:
max_num = new_num
# 还原比较对象
charsB = list(random)
print("交换后:" + str(max_num))
package cloud.cue.group.util; import java.util.Random; /** * @Author xingmengyuan * @Date 2023/5/30 */ public class demo { public static void main(String[] args) { String random = getRandom(7); System.out.println("交换前:"+ random); Integer max = 0; char[] charsA = random.toCharArray(); char[] charsB = random.toCharArray(); for (int i = 0; i < random.length(); i++) { char temp = charsA[i]; if(temp == 0){ continue; } for (int j = i+1; j < random.length(); j++) { if(temp == charsA[j]){ continue; } charsB[i] = charsA[j]; charsB[j] = temp; String tempNum = new String(charsB); Integer newNum = Integer.valueOf(tempNum); if (max.compareTo(newNum) < 0) { max = newNum; } //还原比较对象 charsB = random.toCharArray(); //System.out.println("有效交换次:"+newNum); } } System.out.println("交换后:" + max); } public static String getRandom(int len) { int digit = (int) Math.pow(10, len - 1); int rs = new Random().nextInt(digit * 10); if (rs < digit) { rs += digit; } return String.valueOf(rs); } } 转换成python
import random
def get_random(len):
digit = 10 ** (len - 1)
rs = random.randint(digit, digit * 10 - 1)
if rs < digit:
rs += digit
return str(rs)
def main():
random_str = get_random(7)
print("交换前:" + random_str)
max_num = 0
chars_a = list(random_str)
chars_b = list(random_str)
for i in range(len(random_str)):
temp = chars_a[i]
if temp == '0':
continue
for j in range(i + 1, len(random_str)):
if temp == chars_a[j]:
continue
chars_b[i] = chars_a[j]
chars_b[j] = temp
temp_num = ''.join(chars_b)
new_num = int(temp_num)
if max_num < new_num:
max_num = new_num
# 还原比较对象
chars_b = list(random_str)
# print("有效交换次:" + str(new_num))
print("交换后:" + str(max_num))
if __name__ == '__main__':
main()
阅读全文