shell学习之有关【随机数】的面试题练习

1)使用for循环在/westos目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串westos

#!/bin/bash

Path=/westos

[ -d "$Path" ] || mkdir -p $Path

for i in `seq 10`
do
    random=$(openssl rand -base64 40 | sed 's/[^a-z]//g' | cut -c 3-12)
    touch $Path/${random}_westos.html
done

2) 创建10个用户westos(01-10),并设置其密码为10位随机数

[root@server tmp]# cat random.sh 
#!/bin/bash

. /etc/init.d/functions

user="westos"
passfile="/tmp/user.log"

for num in `seq -w 10`
do
    pass="`echo $RANDOM | md5sum | cut -c 3-12`"
    useradd $user$num &> /dev/null && {
        echo "$pass" | passwd --stdin $user$num &> /dev/null
        echo -e "user:$user$num\tpasswd:$pass" >> $passfile
    }

if [ $? -eq 0 ];then
    action "$user$num is ok" /bin/true
else
    action "$user$num is failed" /bin/false
fi
done
[root@server tmp]# sh random.sh 
westos01 is ok                                             [  OK  ]
westos02 is ok                                             [  OK  ]
westos03 is ok                                             [  OK  ]
westos04 is ok                                             [  OK  ]
westos05 is ok                                             [  OK  ]
westos06 is ok                                             [  OK  ]
westos07 is ok                                             [  OK  ]
westos08 is ok                                             [  OK  ]
westos09 is ok                                             [  OK  ]
westos10 is ok                                             [  OK  ]
[root@server tmp]# cat /tmp/user.log
user:westos01	passwd:7fdb143dd9
user:westos02	passwd:6476ef99ad
user:westos03	passwd:3962f843f4
user:westos04	passwd:feb2f5d496
user:westos05	passwd:7890eeabcf
user:westos06	passwd:fa10a849f4
user:westos07	passwd:a740ee29f3
user:westos08	passwd:87e0eb374c
user:westos09	passwd:293d9243f0
user:westos10	passwd:878c4df9f4

当谈到C++中的随机数面试题时,以下是一些常见的问题和答案: 问题1:如何在C++中生成随机数? 答案:在C++中,可以使用标准库中的`<random>`头文件来生成随机数。通常使用以下步骤: - 创建一个随机数引擎对象,如`std::default_random_engine`。 - 选择合适的随机数分布,如均匀分布`std::uniform_int_distribution`或正态分布`std::normal_distribution`。 - 使用随机数引擎和随机数分布来生成随机数。 问题2:如何生成一个范围在[min, max]之间的随机整数? 答案:可以使用`std::uniform_int_distribution`来实现。以下是一个示例代码: ```cpp #include <iostream> #include <random> int main() { int min = 1; int max = 10; std::random_device rd; std::default_random_engine engine(rd()); std::uniform_int_distribution<int> distribution(min, max); int randomNum = distribution(engine); std::cout << "Random number: " << randomNum << std::endl; return 0; } ``` 问题3:如何生成一个范围在[0, 1)之间的随机浮点数? 答案:可以使用`std::uniform_real_distribution`来实现。以下是一个示例代码: ```cpp #include <iostream> #include <random> int main() { std::random_device rd; std::default_random_engine engine(rd()); std::uniform_real_distribution<double> distribution(0.0, 1.0); double randomNum = distribution(engine); std::cout << "Random number: " << randomNum << std::endl; return 0; } ``` 这些是一些常见的C++随机数面试题,希望对你有所帮助!如有更多问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值