0

I want to create a script that will create random numbers from 0 to 30,000 and save them in a text file.

I want all the numbers to be in one line. I also want the numbers to be through 1 to 1,000,000 (I mean there should be 1 to 1,000,000 numbers in the file).

How do I do it? I want that script to test a program I made, and I want a script to automatically create numbers so I don't need to take time to write them on my own.

I also don't know anything about Linux scripting so if you can add some comments in your code I would be gladly accepted! :)

7
  • is that a homework? or do you want to learn shell scripting/programming?
    – AdigaJo
    Oct 30, 2014 at 21:26
  • neither. I want to make my life simplier :). But i need to start someday. You also know that scripting in linux can help you a lot in those small problems!
    – Nikos KLon
    Oct 30, 2014 at 21:27
  • 1
    From 1 to 32768 is pretty simple: use the $RANDOM variable.
    – muru
    Oct 30, 2014 at 21:28
  • ok and for the others? But I want only those numbers not just randoms.
    – Nikos KLon
    Oct 30, 2014 at 21:29
  • @NikosKLon so you want all numbers from 1 to N but shuffled (in a random order)?
    – muru
    Oct 30, 2014 at 21:30

3 Answers 3

2

Use the shuf command:

shuf -e {1..N} | tr '\n' ' ' > some-file

For example:

$ shuf -e {1..10} | tr '\n' ' '
4 9 1 7 8 6 3 5 10 2

For very large numbers you might want to use a for loop and then shuffle it:

for i in {1..1000000}
do 
    echo $i
done |
shuf | tr '\n' ' ' > some-file
8
  • ok so we create large numbers until 1000000 right?
    – Nikos KLon
    Oct 30, 2014 at 21:37
  • @NikosKLon all numbers from 1 to N. Isn't that what you said here: askubuntu.com/questions/543799/…?
    – muru
    Oct 30, 2014 at 21:39
  • it will have all the numbers from 1 to N without repeat randomized. if you want them in the same line with spaces replace '\n' with ' '
    – AdigaJo
    Oct 30, 2014 at 21:40
  • thanks. How mnay of those will be created? eg If I want to create only 3 number or 500.000 how can I?
    – Nikos KLon
    Oct 30, 2014 at 21:40
  • @NikosKLon each number exactly once. For larger numbers use the loop. I'm not sure what you mean by "only 3 numbers".
    – muru
    Oct 30, 2014 at 21:41
2
#!/usr/bin/env python
# The line above should always be on the top of
# an executable python file. It describes the 
# program this file should be interpreted with

# Import let's you extend the core functunality
# of python with code in extra "modules"
import random

# result is a string variable. Python is not strict
# about variable declaration or typing. They can
# be declared like this at any point in the programm 
# and may hold a variaty of data types. This one holds
# text now.
result = ""

# The imported module random offers the function randint.
# This function generates a number, where: 1 <= n <= 1000000
loop_iterations = random.randint(1,1000000)

# counter starts of at zero, counts up and, 
# on the last round, reaches loop_iterations -1
for counter in range(loop_iterations):
    new_random_integer = random.randint(0,30000)
    result += ", "
    # We can't add a number to a string, so we need
    # to convert it into a text representation first.
    result += str(new_random_integer)

# A file handler let's you interact with an opened file.
# open takes a path and a setting wether you want to 
# read/write. w means write.
file_handler = open('/tmp/output.txt', 'w')
file_handler.write(result)
file_handler.close()
1

A very frequent technique for generating random numbers within specific range to is take random integer , and compute the remainder from integer division between the integer and the difference of the range limits. Basic idea is that remainder can only be from 0 to divisor, so we get that and then offset the resulting value by the low limit. I've used this technique multiple times when writing code in C.

Of course, this question is tagged with scripts and it is well possible to implement the same technique in bash. We can use $RANDOM variable generator and then take the integer into desired range using bc calculator. The script below does exactly that:

#!/usr/bin/env bash

get_random_int(){

    echo "$low + $RANDOM % ( $high - $low + 1  )" | bc
}

main(){
    local howmany=$1
    local low=$2
    local high=$3

    local counter=0    
    while [ $counter -lt $howmany  ]
    do
        get_random_int
        counter=$(($counter+1))
    done
}

main "$@"

Basic usage:

$ ./random_in_range.sh 3 100 200                                                                                                      
184
110
179

The first argument is how many integers you want, second is low limit, and third argument is upper limit.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .