如何在bash中生成一千万个随机字符串 [英] How to generate 10 million random strings in bash

查看:71
本文介绍了如何在bash中生成一千万个随机字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为排序算法制作一个很大的测试文件.为此,我需要生成1000万个随机字符串.我怎么做?我尝试在/dev/urandom上使用cat,但是它持续运行了几分钟,当我查看文件时,只有大约8页的字符串.我如何在bash中生成1000万个字符串?字符串应为10个字符长.

I need to make a big test file for a sorting algorithm. For that I need to generate 10 million random strings. How do I do that? I tried using cat on /dev/urandom but it keeps going for minutes and when I look in the file, there are only around 8 pages of strings. How do I generate 10 million strings in bash? Strings should be 10 characters long.

推荐答案

这不能保证唯一性,但是可以在文件中提供1000万条随机行.不太快,但是在我的机器上跑了不到30秒:

This will not guarantee uniquness, but gives you 10 million random lines in a file. Not too fast, but ran in under 30 sec on my machine:

cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 10000000 > file

这篇关于如何在bash中生成一千万个随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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