Quiero generar, digamos 10 números aleatorios entre 100 y 200 (ambos incluidos). ¿Cómo lo hago con rand?
Quiero generar, digamos 10 números aleatorios entre 100 y 200 (ambos incluidos). ¿Cómo lo hago con rand?
Respuestas:
Si te refieres a la randde la randpaquete (en contraposición a la de OpenSSL), no es compatible con un límite inferior, solamente un límite superior. Lo que puedes hacer es el truco shift-lower-bound-to-zero-then-add-lower-bound:
$ rand -N 10 -M 100 -e -d '\n' | awk '{$0 += 100}1'
170
180
192
168
169
170
117
180
167
142
-N es la cantidad de números aleatorios que necesitas-Msería el límite superior de las randsalidas de números , entonces ( max - min = 100)-e -d '\n'establece el delimitador en una nueva línea. Eso es por conveniencia de procesamiento por awk.El awkcódigo luego toma cada línea y le agrega 100.
Utilizando python:
#!/usr/bin/env python2
import random
for i in range(10):
print random.randint(100, 200)
Salida:
187
123
194
190
124
121
191
103
126
192
Aquí he usado el randommódulo de pythonpara generar 10 ( range(10)) enteros aleatorios entre 100 y 200 (incluidos) ( random.randint(100, 200)).
Aquí hay una manera de 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
También podría usar /dev/urandom(adaptado de aquí ):
$ 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
Con shufGNU coreutils:
$ shuf -i 100-200 -n 10
159
112
192
140
166
121
135
120
198
139
Puedes usar $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
Nada espectacular: simple emulación de for loop con icontador y while. El rango se establece usando la if . . . else . . .fiestructura. Nota al margen: mi indicador es primero el directorio de trabajo, luego el área de entrada, así que no se confunda con lo que ve
$ ./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
Este es el código que he estado usando mientras tomé la clase C en mi universidad. La pequeña edición que agregué para ajustarse a esta pregunta es el uso de argumentos de línea de comandos (en lugar de codificar valores en la fuente)
$ 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;
}
randombiblioteca: stackoverflow.com/a/19728404/2072269 , stackoverflow.com/a/19666713/2072269
rand? LaRANDOMvariable?/dev/random?/dev/urandom?