Tag Info

Hot answers tagged

23

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


22

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.


19

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


19

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 ...


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 ...


18

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 ...


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 ...


16

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 ...


15

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 ...


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

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 ...


12

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.


10

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 ...


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

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, ...


6

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


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, ...


6

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 ...


5

Why does dd drop data? ... Gilles has posed this engaging question about dd: When is dd suitable for copying data? (or, when are read() and write() partial) Here is an excerpt from that question:     ...it's not difficult to put dd at fault; for example try this code:*         yes | dd of=out bs=1024k count=10     and check the size of the out file (it's ...


5

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. ...


5

You misunderstand regex syntax. [16-32] does not mean "match 16, 17, ... or 32". It means "match one character which is either 1 or 2 or in the range 6-3" (which is not a valid range, hence the error). It's possible to write a regex to match a range of integers, but it's complex and error prone. In your case, it would be much easier to use nmap's ...


5

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 ...


4

If you want finer-grained control than maxschlepzig's nice bash incantations, it's a reasonably easy thing to just code up: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <time.h> int main(int argc, char**argv){ useconds_t mdelay=0, delay; if (argc<3){ fprintf(stderr,"%s <delay (in ...


4

find . -type f | shuf | # shuffle the input lines, i.e. apply a random permutation nl -n rz | # add line numbers 000001, … while read -r number name; do ext=${name##*/} # try to retain the file name extension case $ext in *.*) ext=.${ext##*.};; *) ext=;; esac mv "$name" "../randomized/${name%/*}/$number$ext" done Replace mv by ln or ln ...


4

You can do something like this in Bash: $ (( RANDOM%2 == 0 )) && C1 || C2 This will generate a random number, either 0 or 1. If it's a 0, then C1 runs, otherwise C2 runs if it isn't. example $ (( RANDOM%2 == 0 )) && echo 0 || echo 1 1 $ (( RANDOM%2 == 0 )) && echo 0 || echo 1 0 NOTE: The first character, $, is the prompt. ...


4

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 ...


4

Entropy is not only lost via /dev/{,u}random, the kernel also takes some. For example, new processes have randomized addresses (ASLR) and network packets need random sequence numbers. Even the filesystem module may remove some entropy. See the comments in drivers/char/random.c. If you need to watch the entrophy pool, do not use watch cat, that will consume ...


4

If I had to apply that shuffling to a deck of playing card, I think I'd first shuffle the deck, then display the cards in a row before my eyes and processing from left to right, wherever there are adjacent clubs or heart... move all but one of those at random somewhere else (though not next to another one of the same type). For example, with a hand like 🂡 ...



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