LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 12-15-2006, 09:00 AM   #1
koedil
LQ Newbie
 
Registered: Dec 2006
Distribution: FC5
Posts: 4

Rep: Reputation: 0
random counter


hii all,


i'm a newbie
and i need a program random counter that work under linux, can somebody help me out..

what i need is a random counter for numeric up to 14 digits like in a tv quiz program, which going to
include (my client is one of our local bank) customer mobile phone number or their bank account number in order to get the prize

if you could help me on how or where i can get the source/script/whatsoever..
regarding that matter i would mostly appreciate it..

Last edited by koedil; 12-15-2006 at 09:07 AM.
 
Old 12-16-2006, 06:48 PM   #2
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Why don't you just throw darts at a number board? It would be quicker and easier.
 
Old 12-17-2006, 06:53 AM   #3
koedil
LQ Newbie
 
Registered: Dec 2006
Distribution: FC5
Posts: 4

Original Poster
Rep: Reputation: 0
darts?.. maybe for my next client..
now, how about some serious help.. about that random counter things..
will you help ?

my clients wants a software that could link to their database, on this tv show program (every 3 months)..
if i managed to run this program smoothly.. my client's aggreed to migrating the whole management system to linux

So? Help?

ps.
thanks for replying
(including that dart things..)
 
Old 12-17-2006, 10:40 AM   #4
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Smile

Here is a solution that I did interactively in the bash shell. Each call will result in a random number whose value will fall in the range 0 to 9. Do this once for each digit.

Code:
echo $(( $RANDOM / 3276 ))
8
echo $(( $RANDOM / 3276 ))
6
echo $(( $RANDOM / 3276 ))
6
echo $(( $RANDOM / 3276 ))
7
echo $(( $RANDOM / 3276 ))
2
echo $(( $RANDOM / 3276 ))
4
echo $(( $RANDOM / 3276 ))
1
echo $(( $RANDOM / 3276 ))
2
echo $(( $RANDOM / 3276 ))
5
echo $(( $RANDOM / 3276 ))
7
echo $(( $RANDOM / 3276 ))
0
Okay, so that's just eleven digits.

You can put this in a shell script by putting the correct shell call at the top of the script and making the script executable.
Code:
#!/bin/bash
# This is a bash script to create a fourteen character string of random numeric digits.
echo -n "The random number is "
for ((i=1; i<15; i++)) ; do
echo -n $(( $RANDOM / 3276 ))
done
echo
You can store the random number in a variable as follows:
Code:
#!/bin/bash
# This is a bash script to create a fourteen character string of random numeric digits.
unset x
for ((i=1; i<15; i++)) ; do
x=$x$(( $RANDOM / 3276 ))
done
echo The random number is $x
echo
This is the theory behind this code.

The bash shell has a built in random number generator in the form of a shell variable named RANDOM. Each call to RANDOM will generate a random number in the value range of 0 to 32767. Since we want a value of 0 to 9 we have to divide the result of the call to RANDOM by 3276. This will result in an integer whose value will be in the range 0 to 9. This is the only really interesting part of the code. It is interesting because we are taking a tool that we are given that does what we want to do but the result is in the wrong range so we alter the result to fit our requirement. One potential problem with making this sort of an adjustment is that the code may fail to generate one or both of the values at the ends of the desired range. I have run this code a few dozen times and I have observed that both zeros and nines are generated. You always have to test your code. I have found that testing code with values that are at the extreme ends of the data set is critical. (In this case the data set is the range of digits from 0 to 9. The test is to see if these values are ever generated.) This is because of a common programming error referred to as an "off by one" error. Lots of code suffers from this problem and it can remain hidden unless you specifically test for it. If you run the program a bunch of times you should see some occurrences of zero and some occurrences of nine being generated. That means that our code is okay.

We have to put the dollar sign in front of the pseudo-variable named RANDOM so that we can read the value of this variable as per the rules of bash.

The for loop makes the code more compact. Each iteration of the for loop will generate one digit so we execute the loop 14 times to create 14 digits.

In the first example we print the result of each loop iteration immediately without allowing a line feed between each call to echo. This results in a single 14 digit number being printed on one line. In the second example we accumulate the results of each loop iteration in a variable named x and print the entire completed random number in one call to the echo function once the entire number has been created. The second approach has two advantages over the first approach. The second approach has less computer I/O so it is less work for the computer, and the second approach allows us to store the random number and use it elsewhere.

Happy random number generating.


Last edited by stress_junkie; 12-17-2006 at 11:59 AM.
 
Old 12-18-2006, 02:27 AM   #5
koedil
LQ Newbie
 
Registered: Dec 2006
Distribution: FC5
Posts: 4

Original Poster
Rep: Reputation: 0
thank you for the solution..
i'll get on to the bottom of this, since my client has 175.000 customer in their bank and we need to get the whole things worked by dec 28th..so it's going to be quite a lot of work for a newbie..
we're only going to use the last 6 number out of 14, and picked only 50 number (out of 175.000) to come out as winner after we scrambles the number which the process going to be shown in a big screen projector, and there will be lawyers, the top level bank management , the third party, the customers, the tv crews (yes, it's going to be a circus!!).. to witness the whole thing will run fair and square.

so once again thank you so much for the attention, the effort, the kindness..
i wish we live nearby so i can buy you beers and we can play darts
keep in touch (if something occur you might be the first person i look for emergency.. hehe)

sincerely
-koedil-
 
  


Reply

Tags
random number


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for a traffic counter vahan Linux - Networking 4 03-11-2006 09:53 AM
hit counter dragon Programming 6 07-04-2005 10:06 PM
Counter-strike,[color=orange]H L F - L I F E [/color],Counter-strike: Source. Dennisgoop Linux - Games 2 08-10-2004 03:03 AM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 12:12 PM
Counter AMMullan Programming 5 10-23-2003 02:11 PM


All times are GMT -5. The time now is 01:49 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration