Tag Info

Hot answers tagged

28

On most unices: head -c 1M </dev/urandom >myfile If your head doesn't understand the M suffix: head -c 1048576 </dev/urandom >myfile If your head doesn't understand the -c option (it's common but not POSIX; you probably have OpenBSD): dd bs=1024 count=1024 </dev/urandom >myfile Do not use /dev/random on Linux, use /dev/urandom.


27

if (( RANDOM % 2 )); then C1; else C2; fi


21

Based on the error message that you get, I don't think /dev/urandom is the problem. If it were, I'd expect an error like "no such file or directory". I searched for the error message you got and found this, which seems like it might be relevant to your issue: http://nerdbynature.de/s9y/?176 Basically, specify the locale by prepending the tr command with ...


21

In your special case: C$((RANDOM%2+1)) will work :) And hey, it's the shortest answer!


20

It writes until the disk is full (usually there is still some space reserved for the root user). But as the pool of random data is limited, this could take a while. If you need a certain amount of random data, use dd. For 1MB: dd if=/dev/random iflag=fullblock of=$HOME/randomFile bs=1M count=1 Other possibilities are mentioned in answers to a related ...


20

That's exactly the difference between /dev/random and /dev/urandom -- random uses the entropy pool, which gathers noise from a bunch of sources and keeps track of "how much" noise is currently in the pool, so random knows how much high-quality randomness it can generate. Since the entropy pool has a finite amount of noise, reading from random might need to ...


20

It will eventually. In: cat /dev/random | strings --bytes 1 | tr -d '\n\t ' cat will never buffer, but it's superfluous anyway as there's nothing to concatenate here. < /dev/random strings --bytes 1 | tr -d '\n\t ' strings though, since its output is not longer a terminal will buffer its output by blocks (of something like 4 or 8kB) as opposed to ...


19

You're observing a combination of the peculiar behavior of dd with the peculiar behavior of Linux's /dev/random. Both, by the way, are rarely the right tool for the job. Linux's /dev/random returns data sparingly. It is based on the assumption that the entropy in the pseudorandom number generator is extinguished at a very fast rate. Since gathering new ...


18

Assuming that pseudo-random data is sufficient, dd if=/dev/urandom of=target-file bs=1M count=1000000 will do what you want. dd(1) will read blocks of data from an input file and write them to an output file. The command line language is a little quirky, but it is one of those really useful tools worth mastering the basics of. In this case if is input ...


17

You can write to /dev/random because it is part of the way to provide extra random bytes to /dev/random, but it is not sufficient, you also have to notify the system that there is additional entropy via an ioctl() call. I needed the same functionality for testing my smartcard setup program, as I did not want to wait for my mouse/keyboard to generate enough ...


14

Linux has two random number generators available to userspace, /dev/random and /dev/urandom. /dev/random is a source of "true" randomness - i.e. it is not generated by a pseudo-random number generator. Entropy is fed into this by the input driver and the interrupt handler, through the functions add_input_randomness and add_interrupt_randomness. Processes ...


13

Typically, it's designed by kernel developers and documented in man 4 random: Writing to /dev/random or /dev/urandom will update the entropy pool with the data written, but this will not result in a higher entropy count. This means that it will impact the contents read from both files, but it will not make reads from /dev/random faster.


12

Use this: nl file | shuf -n2 | sort -n | cut -f2- nl to number the lines, shuf to shuffle and limit the output to 2 lines (-n), sort to rebuild the original order, and cut to remove the numeration of nl. It will print 2 lines of your file in the original order of the file. Use shuf -n X, where X can be any number.


11

From man 4 random on a RHEL 5 box: When read, the /dev/random device will only return random bytes within the estimated number of bits of noise in the entropy pool. I get files of size 213 bytes on that machine. Back to man 4 random: When read, /dev/urandom device will return as many bytes as are requested. I get 2048 bytes from ...


10

shuf is the command you are looking for. From man shuf, -n, --head-count=COUNT output at most COUNT lines So, for example to get 4 random lines from the file, you could use the command as, shuf -n 4 file You could even use the below approach. head -$((${RANDOM} % `wc -l < file` + 1)) file | tail -1 Where, the final pipe to ...


9

You can try shuf from GNU coreutils: shuf -i 1-100 -n 1


9

On a GNU system: find / -type d -print0 | shuf -zn5 | xargs -r0n1 cp foo (now copying the file to things like /sys or /proc would not make sense or even be possible, you may want to add -xdev to only select directories on the file system mounted at /). You could make it compatible with both FreeBSD and GNU with: find / -type d -print0 | sort -zR | tr ...


8

Your tr attempts to interpret its input as text in UTF-8 encoding. So it will complain and abort upon the first byte sequence which is not valid UTF-8. Prefixing tr with LC_ALL=C or LC_CTYPE=C will export that variable into the environment of tr, thus changing its idea of the local character set to the C standard, i.e. everything is just a sequence of opaque ...


8

Summary: dd is a cranky tool which is hard to use correctly. Don't use it, despite the numerous tutorials that tell you so. dd has a “unix street cred” vibe attached to it — but if you truly understand what you're doing, you'll know that you shouldn't be touching it with a 10-foot pole. dd makes a single call to the read system call per block (defined by ...


8

You can do this with coreutils' sort with any of the following: sort -R file sort --random-sort file sort --sort=random file from man sort: -R, --random-sort sort by random hash of keys --sort=WORD sort according to WORD: general-numeric -g, human-numeric -h, month -M, numeric -n, random -R, version -V


7

See man bash, PARAMETERS section, Shell Variables subsection: RANDOM Each time this parameter is referenced, a random integer between 0 and 32767 is generated. The sequence of random numbers may be initialized by assigning a value to RANDOM. If RANDOM is unset, it loses its special properties, even if it is subsequently reset. ...


7

http://www.fourmilab.ch/random/ works for me. sudo apt-get install ent head -c 1M /dev/urandom > /tmp/out ent /tmp/out


7

Your example data and constraints actually only allow a few solutions—you must play John B. every other song, for example. I'm going to assume your actual full playlist isn't essentially John B, with random other stuff to break it up. This is another random approach. Unlike @frostschutz's solution, it runs quickly. It does not guarantee a result that ...


7

GnuPG consumes several bytes from /dev/random for each random byte it actually uses. You can easily check that with this command: start cmd:> strace -e trace=open,read gpg --armor --gen-random 2 16 2>&1 | tail open("/etc/gcrypt/rngseed", O_RDONLY) = -1 ENOENT (No such file or directory) open("/dev/urandom", O_RDONLY) = 3 read(3, ...


7

There is a grain of truth to this, in fact more truth than myth, but nonetheless the statement reflects a fundamental misunderstanding of what's going on. Yes, moving the mouse while generating a key with GPG can be a good idea. Yes, moving the mouse contributes some entropy that makes random numbers random. No, moving the mouse does not make the key more ...


7

How about fold? It's part of coreutils... $ tr -dc 01 < /dev/urandom | fold -w 30 | head -n 5 001010000111110001100101101101 000101110011011100100101111000 111010101011100101010110111001 111011000000000101111110110100 110011010111001110011010100011 Or if that's not available, some flavour of awk: $ tr -dc 01 < /dev/urandom | awk \$0=RT RS=.\{,30} | ...


7

Use single quotes instead of double quotes: alias rdir='mkdir -p ./$(cat /dev/urandom | tr -cd 'a-z0-9' | head -c 8)/' Now, the statement is evaluated every time the alias is called. With double quotes the statement is evaluated, when defining the alias, therefore static. Also a simpler solution to create a random directory inside the current working ...


6

In bash you can do it like this: sleep $(($RANDOM%3)) && some_command thus waiting between 0 and 2 seconds before executing the command. Or choose another interval. Or if $RANDOM is not available you can try jot: sleep `jot -r 1 0 3` && some_command Sure, the random waiting plus executing is not provided by a single binary, but close. ...


6

You are writing 512 bytes into a file and execute it. So the outcome could be anything a program with 512 bytes could possibly do. What that is depends on your machine. But 512 bytes are plenty of instructions, so basically everything could have happened like changing the root password, creating random files or generating a tar archive containing the source ...


6

Your suggestion that this difference is because openssl uses /dev/urandom and gpg uses /dev/random is correct. You can watch the available entropy going down while generating keys with gpg using: watch -n 1 cat /proc/sys/kernel/random/entropy_avail I used a program for generating the description of the steps for setting up a OpenGPG smart card with gpg, ...



Only top voted, non community-wiki answers of a minimum length are eligible