# random function script

**URL:** <https://community.unix.com/t/random-function-script/165138>\
**Category:** Shell Programming and Scripting\
**Created:** [September 14, 2006, 9:09am UTC](https://community.unix.com/t/random-function-script/165138 "2006-09-14T09:09:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![prash\_b](https://community.unix.com/letter_avatar/prash_b/32/5_5575768a8748004e209b776fc1b2916d.png) [@prash\_b](https://community.unix.com/u/prash_b)\
**Post date:** [September 14, 2006, 9:09am UTC](https://community.unix.com/t/random-function-script/165138/1 "2006-09-14T09:09:03Z")

</div>

hi,

I need a command which picks the records randomly from the file.  
For example. i am having some 10000 entries in a file and need to extract the lines randomly without repeating the numbers.

Do anybody have any idea on how to get this out.

---

<div class="post-metadata">

**Author:** ![Yogesh\_Sawant](https://community.unix.com/user_avatar/community.unix.com/yogesh_sawant/32/22562_2.png) [@Yogesh\_Sawant](https://community.unix.com/u/Yogesh_Sawant)\
**Post date:** [September 14, 2006, 9:41am UTC](https://community.unix.com/t/random-function-script/165138/2 "2006-09-14T09:41:55Z")

</div>

#!/bin/bash

RANGE=1000  
number=$RANDOM  
let "number %= $RANGE"  
echo "Random number less than $RANGE --- $number"

---

<div class="post-metadata">

**Author:** ![ghostdog74](https://community.unix.com/letter_avatar/ghostdog74/32/5_5575768a8748004e209b776fc1b2916d.png) [@ghostdog74](https://community.unix.com/u/ghostdog74)\
**Post date:** [September 14, 2006, 9:52am UTC](https://community.unix.com/t/random-function-script/165138/3 "2006-09-14T09:52:09Z")

</div>

Alternative , in Python:

```nohighlight
>>> import random
>>> length = 10000
>>> all = open("file_with_records.txt").readlines()
>>> for num in range(0,length-1):
... randnum = random.randint(0,length-1) #generate random num
... print all[randnum]

```

---

<div class="post-metadata">

**Author:** ![Yogesh\_Sawant](https://community.unix.com/user_avatar/community.unix.com/yogesh_sawant/32/22562_2.png) [@Yogesh\_Sawant](https://community.unix.com/u/Yogesh_Sawant)\
**Post date:** [September 14, 2006, 10:00am UTC](https://community.unix.com/t/random-function-script/165138/4 "2006-09-14T10:00:11Z")

</div>

#!/usr/bin/perl

use strict;  
my $limit = 10000;

my $random\_number = int (rand $limit);  
print "random number: $random\_number \n";

---

<div class="post-metadata">

**Author:** ![prash\_b](https://community.unix.com/letter_avatar/prash_b/32/5_5575768a8748004e209b776fc1b2916d.png) [@prash\_b](https://community.unix.com/u/prash_b)\
**Post date:** [September 15, 2006, 4:54am UTC](https://community.unix.com/t/random-function-script/165138/5 "2006-09-15T04:54:11Z")

</div>

Thanks a lot,

This is working now 🙂
