如何在csh中随机获取0或1
how to get either 0 or 1 randomly in csh
在bash产生1和0随机数
我正在使用
#!/bin/bash
for i in `seq 1 100000`; do
let rand=$RANDOM%2
echo $i $rand >> r-test.dat
done
并且还在使用,
设置随机数=awk -v min=1000 -v max=10000 'BEGIN{srand(); print int(min+rand()*(max-min+1))}'
产生随机数,例如在csh中1000到10000之间。现在我的问题是如何修改此命令或使用更有效的方法在 csh 中随机生成 0 和 1,就像我从 bash?
中的第一个代码中得到的一样
提前感谢您的评论。
set RAND = `od -vAn -N1 -tu1 < /dev/urandom` ; @ RAND01 = ( $RAND % 2 ) ; echo $RAND01
或者,作为生成 100,000 个 0 和 1 的循环,就像您的 bash 脚本:
#!/bin/csh
cat /dev/urandom # read from kernel's random number generator \
| head -c 100000 # take the first 100,000 bytes \
| od -vAn -tu1 # transform the raw bytes into integer numbers \
| tr ' ' '\n' # put every number on its own line (transform all [SPACE] characters to [LF] characters) \
| grep -v '^ *$' # filter out lines that are blank or only spaces \
> r-test.tmp # store the resulting 100,000 numbers in a temp file
rm -f r-test.dat # reset the output file
foreach RAND ( "`cat r-test.tmp`" ) # for each line in the temp file
@ RAND01 = ( $RAND % 2 ) # use modulo 2 to map every input number to '0' or '1'
echo $RAND01 >> r-test.dat # store the results in an output file
end # end of loop
这是一个 bash
版本,适合在 bash
而不是 csh
工作的人,因为评论、循环和算术有点不同:
#!/bin/bash
cat /dev/urandom | # read from kernel's random number generator
head -c 100000 | # take the first 100,000 bytes
od -vAn -tu1 | # transform the raw bytes into integer numbers
tr ' ' '\n' | # put every number on its own line (transform all [SPACE] characters to [LF] characters)
grep -v '^ *$' | # filter out lines that are blank or only spaces
cat > r-test.tmp # store the resulting 100,000 numbers in a temp file
rm -f r-test.dat # reset the output file
for RAND in `cat r-test.tmp`; do # for each line in the temp file
RAND01=$(( $RAND % 2 )) # use modulo 2 to map every input number to '0' or '1'
echo $RAND01 >> r-test.dat # store the results in an output file
done # end of loop
在bash产生1和0随机数 我正在使用
#!/bin/bash
for i in `seq 1 100000`; do
let rand=$RANDOM%2
echo $i $rand >> r-test.dat
done
并且还在使用,
设置随机数=awk -v min=1000 -v max=10000 'BEGIN{srand(); print int(min+rand()*(max-min+1))}'
产生随机数,例如在csh中1000到10000之间。现在我的问题是如何修改此命令或使用更有效的方法在 csh 中随机生成 0 和 1,就像我从 bash?
中的第一个代码中得到的一样提前感谢您的评论。
set RAND = `od -vAn -N1 -tu1 < /dev/urandom` ; @ RAND01 = ( $RAND % 2 ) ; echo $RAND01
或者,作为生成 100,000 个 0 和 1 的循环,就像您的 bash 脚本:
#!/bin/csh
cat /dev/urandom # read from kernel's random number generator \
| head -c 100000 # take the first 100,000 bytes \
| od -vAn -tu1 # transform the raw bytes into integer numbers \
| tr ' ' '\n' # put every number on its own line (transform all [SPACE] characters to [LF] characters) \
| grep -v '^ *$' # filter out lines that are blank or only spaces \
> r-test.tmp # store the resulting 100,000 numbers in a temp file
rm -f r-test.dat # reset the output file
foreach RAND ( "`cat r-test.tmp`" ) # for each line in the temp file
@ RAND01 = ( $RAND % 2 ) # use modulo 2 to map every input number to '0' or '1'
echo $RAND01 >> r-test.dat # store the results in an output file
end # end of loop
这是一个 bash
版本,适合在 bash
而不是 csh
工作的人,因为评论、循环和算术有点不同:
#!/bin/bash
cat /dev/urandom | # read from kernel's random number generator
head -c 100000 | # take the first 100,000 bytes
od -vAn -tu1 | # transform the raw bytes into integer numbers
tr ' ' '\n' | # put every number on its own line (transform all [SPACE] characters to [LF] characters)
grep -v '^ *$' | # filter out lines that are blank or only spaces
cat > r-test.tmp # store the resulting 100,000 numbers in a temp file
rm -f r-test.dat # reset the output file
for RAND in `cat r-test.tmp`; do # for each line in the temp file
RAND01=$(( $RAND % 2 )) # use modulo 2 to map every input number to '0' or '1'
echo $RAND01 >> r-test.dat # store the results in an output file
done # end of loop