生成范围内的几个随机数


Answers:


7

如果你指的是randrand(而不是从OpenSSL的一个),它不支持的下界,只是一个上限。您可以做的是将下限绑定到零,然后添加下限技巧:

$ rand -N 10 -M 100 -e -d '\n' | awk '{$0 += 100}1'
170
180
192
168
169
170
117
180
167
142
  • -N 是您需要的随机数
  • -M将是数字rand输出的上限,因此(max - min = 100)
  • -e -d '\n'将定界符设置为换行符。这是为了方便处理awk

然后,awk代码将每一行加到100。


太好了,木鲁 而已!Tks很多。
伊格纳西奥·卡维德斯(Ignacio Caviedes)2015年

奎斯特装腔作势,幻想曲,木鲁。Muchas gracias。
伊格纳西奥·卡维德斯(Ignacio Caviedes)2015年

6

使用python

#!/usr/bin/env python2
import random
for i in range(10):
    print random.randint(100, 200)

输出:

187
123
194
190
124
121
191
103
126
192

在这里,我使用的random模块python来生成range(10)介于100和200(包括在内)之间的10()个随机整数(random.randint(100, 200))。


5

这是一种Perl方式:

$ perl -le 'print 100+int(rand(101)) for(1..10)'

129
197
127
167
116
134
143
134
122
117
Or, on the same line:

$ perl -e 'print 100+int(rand(101))." " for(1..10); print "\n"'
147 181 146 115 126 116 154 112 100 116 

您还可以使用/dev/urandom(从此处改编):

$ for((i=0;i<=10;i++)); do 
    echo $(( 100+(`od -An -N2 -i /dev/urandom` )%(101))); 
done
101
156
102
190
152
130
178
165
186
173
143

@ignaciocaviedes不客气。但是,请考虑接受以下答案之一(也许是使用您想要的工具的专家),然后对其余答案进行投票,而不要留下谢谢您的评论。这就是在此表达感谢的方式。您在Castellano al Muru上的西班牙语?Eltíoes de India!:P
特登

@terdon我不知道你刚才说了什么。Oo
Muru,2015年

4

您也可以使用awk

$ awk -v min=100 -v max=200 -v freq=10 'BEGIN{srand(); for(i=0;i<freq;i++)print int(min+rand()*(max-min+1))}'
132
131
148
100
104
125
103
197
184
165


2

您可以使用$RANDOM

number=0   #initialize the number
FLOOR=100
RANGE=200
while [ "$number" -le $FLOOR ]
do
  number=$RANDOM
  let "number %= $RANGE"  # Scales $number down within $RANGE.
done
echo "Random number between $FLOOR and $RANGE $number"
echo

一切正常,Pabi。再次很多。
伊格纳西奥·卡维德斯

1

重击版本

没什么了不起的-用i计数器和计数器进行for循环的简单仿真while。范围是使用if . . . else . . .fi结构设置的。旁注:我的提示首先是工作目录,然后是输入区域,因此请不要对您看到的内容感到困惑

$ ./bashRadom.sh 100 200                                                       
190
111
101
158
171
197
199
147
142
125

bashRadom.sh

#! /bin/bash

i=0; 
while [ $i -lt 10  ]; do 

  NUM=$RANDOM; 
  if [ $NUM -gt $1 ] && [ $NUM -lt $2  ]; then 
      echo $NUM; 
  else continue; 
  fi;
  i=$((i+1));

done

C版

这是我在大学上C课时一直在使用的代码。我为满足此问题而添加的小修改是使用命令行参数(而不是将值硬编码到源代码中)

$ gcc randfunc.c   
$ ./a.out 100 200
100 
106 
155 
132 
161 
130 
110 
195 
105 
162 
187 

randfunc.c

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int randInt (int, int);

void
main (int argc, char *argv[])
{    
  int min = atoi (argv[1]), max = atoi (argv[2]), i = 0;
  srand (time (NULL));    

  for (i; i < 11; i++)
    {
      printf ("%d \n", randInt (min, max));
    }    
}    

int
randInt (int a, int b)
{
  int randValue;
  randValue = a + (int) rand () % (b - a + 1);
  return randValue;    
}


@muru尚未看到有人发布过该帖子。我也正在添加C版本。如果我除了C之外还保留bash版本,那还好吗?
Sergiy Kolodyazhnyy 2015年

无论如何,我都可以。
muru

如果你要使用C,你应该使用C ++与random库:stackoverflow.com/a/19728404/2072269stackoverflow.com/a/19666713/2072269
穆鲁

@muru我不熟悉C ++,到目前为止只熟悉基本C。但感谢您提供信息^ _ ^
Sergiy Kolodyazhnyy 2015年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.