#include#include #include int main(void) { int X=0,Y=9; //X为起始值 Y为终止值 srand( (unsigned)time( NULL ) ); printf("%d\n", rand()%(Y-X+1)+X ); return 0; }
12月
26
#include#include #include int main(void) { int X=0,Y=9; //X为起始值 Y为终止值 srand( (unsigned)time( NULL ) ); printf("%d\n", rand()%(Y-X+1)+X ); return 0; }
使用bash产生n位的随机数。测试中为产生8位。
#!/bin/bash # ------------------------------- # Filename: test.sh # Revision: 1.0 # Date: 2013-08-01 # Author: simonzhang # Email: simon-zzm@163.com # Description: # ------------------------------- function randnum() { di=(0 1 2 3 4 5 6 7 8 9 \ a b c d e f g h i j k l m n o p q r s t u v w x y z \ A B C D E F G H I G K L M N O P Q R S T U V W X Y Z \ ~ ! ^ _) for(( i=0; i<$1; i++)) do num=$num`echo -n ${di[$RANDOM % ${#di[*]}]}` done echo $num } get=$(randnum 8) echo $get
比较简单,不做解释。
package main import( "fmt" "time" "math/rand" ) func main(){ //获取unix time var a string a = fmt.Sprint(time.Now().UnixNano()) //获取随机数 var b string ra := rand.New(rand.NewSource(time.Now().UnixNano())) b= fmt.Sprint(ra.Intn(10)) //打印结果 fmt.Println(a) fmt.Println(b) }