Generate 16 digit positive random Numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Generate 16 digit positive random Numbers
# 1  
Old 08-16-2012
Generate 16 digit positive random Numbers

Hi Unix Gurus,

I have a requirement to generate positive random 16 and 13 digit numbers.

Here is the script I have so far.....

Code:
number=$RANDOM$RANDOM$RANDOM$RANDOM;
let "number %= 10000000000000";
echo $number

But sometimes it is generating negative numbers and also 15 digit numbers.
Please help me fix this or a script which works.

Thanks in Advance !
# 2  
Old 08-16-2012
You know that these aren't true random numbers, don't you?
You cannot generate TRN without dedicated hardware like this:
Quantis RNG - True Random Number Generator - Overview.

However: are leading zeroes acceptable? Is 0123456789110077 acceptable?
If so (for 16 digits numbers):
Code:
range=10;
number="";
for i in {0..15}; do
  r=$RANDOM;
  let "r %= $range";
  number="$number""$r";
done;
echo $number

If, on the contrary, leading zeroes are not acceptable (for 16 digits numbers):
Code:
number=$RANDOM;
let "number %= 9";
let "number = number + 1";
range=10;
for i in {1..15}; do
  r=$RANDOM;
  let "r %= $range";
  number="$number""$r";
done;
echo $number

For 13 digits numbers, you can easily arrange the above solutions.
--
Bye
This User Gave Thanks to Lem For This Post:
# 3  
Old 08-16-2012
Thanks Lem, that helps. Leading zeroes are acceptable.
# 4  
Old 08-16-2012
What Shell are you using? What Operating System are you running?

The largest integer number which can be used in standard Shell arithmetic is:
((2*1024*1024*1024) -1 ) = 2147483647

This is only 10 digits and it will turn negative if you exceed that number.
Therefore if you don't have a better means of working with numbers you will need to use strings and the bc command.

Last edited by methyl; 08-16-2012 at 02:59 PM..
# 5  
Old 08-17-2012
Hi.

Here is a perl solution:
Code:
#!/usr/bin/env perl

# @(#) p1	Demonstrate random big integers, perl, random.org.
# Using module from cpan.org:
# Math::BigInt::Random, q.v.

use Math::BigInt;
use Math::BigInt::Random qw/ random_bigint /;

my ( $i, $number );

print "\n";
for ( $i = 1; $i <= 10; $i++ ) {
  $number = random_bigint( length => 13 );
  print " Big integer, length 13-decimal, # $i: $number\n";
}

print "\n";
for ( $i = 1; $i <= 10; $i++ ) {
  $number = random_bigint( length => 16 );
  print " Big integer, length 16-decimal, # $i: $number\n";
}

print "\n";
for ( $i = 1; $i <= 10; $i++ ) {
  $number = random_bigint( min => 1, max => 10**16 - 1 );
  print " Big integer, 1 - 2**16-1, # $i: $number\n";
}

print "\n";
for ( $i = 1; $i <= 10; $i++ ) {
  $number = random_bigint( length => 16, use_internet => 1 );
  print " Big integer, from random.org, length 16-decimal, # $i: $number\n";
}

exit(0);

producing:
Code:
% ./p1

 Big integer, length 13-decimal, # 1: 5289843301912
 Big integer, length 13-decimal, # 2: 5131901500099
 Big integer, length 13-decimal, # 3: 2395645168348
 Big integer, length 13-decimal, # 4: 5430633161141
 Big integer, length 13-decimal, # 5: 9357805637434
 Big integer, length 13-decimal, # 6: 1499363029752
 Big integer, length 13-decimal, # 7: 6054280946061
 Big integer, length 13-decimal, # 8: 3660352156516
 Big integer, length 13-decimal, # 9: 4802606276086
 Big integer, length 13-decimal, # 10: 3833418903910

 Big integer, length 16-decimal, # 1: 9705369256666533
 Big integer, length 16-decimal, # 2: 3673996578033120
 Big integer, length 16-decimal, # 3: 8091897092632003
 Big integer, length 16-decimal, # 4: 1457302007140337
 Big integer, length 16-decimal, # 5: 8014569470230008
 Big integer, length 16-decimal, # 6: 5833200500889384
 Big integer, length 16-decimal, # 7: 1796616941692496
 Big integer, length 16-decimal, # 8: 6281243514727502
 Big integer, length 16-decimal, # 9: 3166175172072335
 Big integer, length 16-decimal, # 10: 8303094662675836

 Big integer, 1 - 2**16-1, # 1: 8373969216659148
 Big integer, 1 - 2**16-1, # 2: 6546006739508046
 Big integer, 1 - 2**16-1, # 3: 1286637954039551
 Big integer, 1 - 2**16-1, # 4: 8345572676433835
 Big integer, 1 - 2**16-1, # 5: 2905925453670991
 Big integer, 1 - 2**16-1, # 6: 6195505548239626
 Big integer, 1 - 2**16-1, # 7: 2762366058433696
 Big integer, 1 - 2**16-1, # 8: 8429021814413192
 Big integer, 1 - 2**16-1, # 9: 4990069084726779
 Big integer, 1 - 2**16-1, # 10: 4516295819427199

 Big integer, from random.org, length 16-decimal, # 1: 1694345179253085
 Big integer, from random.org, length 16-decimal, # 2: 2165834290283208
 Big integer, from random.org, length 16-decimal, # 3: 2467216438238563
 Big integer, from random.org, length 16-decimal, # 4: 2204334445952010
 Big integer, from random.org, length 16-decimal, # 5: 6380334714185258
 Big integer, from random.org, length 16-decimal, # 6: 5365790761514186
 Big integer, from random.org, length 16-decimal, # 7: 6583089331520896
 Big integer, from random.org, length 16-decimal, # 8: 7352122857137993
 Big integer, from random.org, length 16-decimal, # 9: 2148627395193063
 Big integer, from random.org, length 16-decimal, # 10: 6847378193318718

The random.org source limits free bits to 1M per IP per day. So, in coding a testing this perl script, I am now down to:
Code:
Current allowance:	818,656 bits
Next free top-up:	181,344 bits
Time till next free top-up:	09:41:06

See RANDOM.ORG - True Random Number Service for details on that aspect of truly random numbers (bits)

Best wishes ... cheers, drl
# 6  
Old 08-18-2012
Code:
tr -dc A-Za-z0-9 </dev/urandom |  head -c 14

These 2 Users Gave Thanks to fpmurphy For This Post:
# 7  
Old 08-18-2012
Hi, fpmurphy.
Quote:
Originally Posted by fpmurphy
Code:
tr -dc A-Za-z0-9 </dev/urandom |  head -c 14

Probably a typo, I had better luck with:
Code:
tr -dc '0-9' </dev/urandom |  head -c 14

producing, for example:
Code:
63443697797894

rather than:
Code:
IzjDQNR6LxvwOE

Users should note also:
Code:
       The kernel random-number generator is designed to produce a small
       amount of high-quality seed material to seed a cryptographic pseudo-
       random number generator (CPRNG).  It is designed for security, not
       speed, and is poorly suited to generating large amounts of random data.
       Users should be very economical in the amount of seed material that
       they read from /dev/urandom (and /dev/random); unnecessarily reading
       large quantities of data from this device will have a negative impact
       on other users of the device.
excerpt from man urandom

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting a file based on negative and positive numbers

I have a file that is pipe delimited and in Column F they have number values, both positive and negative. I need to take the one file I am starting with and split it into two separate files based on negative and positive numbers. What is the command to do so? And then I need to also transfer... (4 Replies)
Discussion started by: cckaiser15
4 Replies

2. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

3. Shell Programming and Scripting

Generate random numbers in script

i want to generate a random number through a script, and even if anyone reads the script, they wont be able to figure out what the random number is. only the person who setup the script would know it. something like this could work: random the full thread is here: ... (13 Replies)
Discussion started by: SkySmart
13 Replies

4. Shell Programming and Scripting

Splitting a file based on positive and negative numbers

Dear All, I have to split a tab delimited file in two files based on the presence of a positive or negative in column number 9 , for example file: A 1 5 erg + 6766 0.9889 0.9817 9.01882 erg inside upstream B 1 8 erg2 + 6766 0.9889 0.9817 -9.22 erg2 inside... (3 Replies)
Discussion started by: paolo.kunder
3 Replies

5. Programming

generate array of random numbers

hi guys, I am writing a c program that generates a two dimensional array to make matrix and a vector of random numbers and perform multiplication. I can't figure out whats wrong with my code. It generates a matrix of random numbers but all the numbers in the vector array is same and so is the... (2 Replies)
Discussion started by: saboture88
2 Replies

6. Shell Programming and Scripting

addition of both positive and negative numbers

Let, I have three numbers +00123.25 -00256.54 +00489.23 I need to sum up all those three numbers, after storing them in three variables (say var1, var2, var3). I used both expr and BC, but they didn't work for me. But, I am not able to sum up them, as I don't have any idea how to... (13 Replies)
Discussion started by: mady135
13 Replies

7. Programming

Generate random number

I saw this formula to generate random number between two specified values in shell script.the following. $(((RANDOM%(max-min+divisibleBy))/divisibleBy*divisibleBy+min)) Give a example in book. Generate random number between 6 and 30.like this. $(((RANDOM%30/3+1)*3)) But I have a... (1 Reply)
Discussion started by: luoluo
1 Replies

8. Shell Programming and Scripting

Perl output with negative and positive numbers

Hello, For my weather station I have made a little perl script to put the data into cacti. The next problem I have. I can only get positive numbers or negative numbers. What do I do: Though a shell scrip I call the perl script. Shell script: #!/bin/sh cat data.txt | stats.pl Perl... (4 Replies)
Discussion started by: rbl-blacklight
4 Replies

9. Programming

generating 16 digit random number in C

Hi, How can we generate 16 digit random nos in C. (10 Replies)
Discussion started by: ajaysahoo
10 Replies

10. Shell Programming and Scripting

Generate a random password

Hello All... Can someone help me generate a random password which will be 7 characters long which contains alpha-numeric characters using shell script. I am looking to store the output of the script that generates the password to a variable within a script and use it as the password. ... (5 Replies)
Discussion started by: chiru_h
5 Replies
Login or Register to Ask a Question