Question on Ramdom Number


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Question on Ramdom Number
# 1  
Old 05-19-2005
Question on Ramdom Number

Hi all,

How do I get a ramdom number from 11 to 20 in every loop (loop runs 1000 times).

please let me know.

Thanks
# 2  
Old 05-19-2005
What shell are you using?
bash has builtin pseudo-random number support.

in bash this:

rnd=$((${RANDOM} %10 + 11))

would assign a random number between 11 and 20 to the variable rnd.

edit: depending on the version, this may also exist in ksh, if I remember correctly it exists in pdksh.
# 3  
Old 05-19-2005
Quote:
Originally Posted by jingi1234
Hi all,

How do I get a ramdom number from 11 to 20 in every loop (loop runs 1000 times).

please let me know.

Thanks
.... and 'loop' refers to what exact programming language/environment? [shell, perl, awk....]
# 4  
Old 05-19-2005
I am in Kshell. I need in kshell in less number of lines Smilie

Thanks
# 5  
Old 05-19-2005
need from 0 to 36. Thanks
# 6  
Old 05-19-2005
Quote:
Originally Posted by jingi1234
I am in Kshell. I need in kshell in less number of lines Smilie

Thanks
non-shell dependent way using awk - one line Smilie:

Code:
   rnd=`nawk 'BEGIN{srand(); printf int(rand() * 100 % 36) }'`


Last edited by vgersh99; 05-19-2005 at 05:24 PM..
# 7  
Old 05-19-2005
vgersh99 -

put your code inside a tight loop, echo $rnd and you'll see the problem with it.

Plus, not everyone has nawk... Smilie Me, for example.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Noob question: How to check the total number of inputs entered by user?

Say I have this line: read -p "Enter 3 numbers: " num1 num2 num3; I want to write a while loop that repeatedly asks for input if the number of inputs entered is not equal to 3. I don't know the correct command to find the number of inputs entered. Help, please? (4 Replies)
Discussion started by: jejemonx
4 Replies

2. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

3. Shell Programming and Scripting

Help with sort word followed by exponential number and numeric number at the same time

Input file: ID_34 2E-69 2324 ID_1 0E0 3254 ID_1 0E0 5434 ID_5 0E0 436 ID_1 1E-14 2524 ID_1 5E-52 46437 ID_3 65E-20 45467 ID_1 0E0 6578 ... Desired output file: ID_1 0E0 6578 ID_1 0E0 5434 ID_1 0E0 3254 ID_1 5E-52 46437 ID_1 1E-14 2524 ID_3 65E-20 45467 (5 Replies)
Discussion started by: cpp_beginner
5 Replies

4. Shell Programming and Scripting

The difference between end number in the early row and the start number in the next

Hi Power User, I'm trying to compute this kind of text file format: file1: jakarta 100 150 jakarta 170 210 beijing 220 250 beijing 260 280 beijing 290 320 new_york 330 350 new_york 370 420 tokyo 430 470 tokyo 480 ... (2 Replies)
Discussion started by: anjas
2 Replies

5. Shell Programming and Scripting

AWK print number of records, divide this number

I would like to print the number of records of 2 files, and divide the two numbers awk '{print NR}' file1 > output1 awk '{print NR}' file2 > output2 paste output1 output2 > output awl '{print $1/$2}' output > output_2 is there a faster way? (8 Replies)
Discussion started by: programmerc
8 Replies

6. Shell Programming and Scripting

changing number in bash (number is in form of string)

I have a txt file as database. when i run my program what it does is it ask me for 3 name and stored in the file as name1:name2:name3:1 when u enter 3 name it add those in file as above format and add 1 at the end. I what i want is if i enter same names again it changes that 1 to 2 and so... (3 Replies)
Discussion started by: Learnerabc
3 Replies

7. Shell Programming and Scripting

Question on awk for finding the column number using a match word

Hi Guys, Please help me out in my situation of writing a shell script Exampl:I have a output like asnapply 1 2 3 apply_server=1 apply_schema=ASN asnapply 1 2 3 apply_server=2 apply_schema=ASN Now i need output like asnacmd applysever=1 applyschema=ASN stop asnacmd applysever=2... (16 Replies)
Discussion started by: mallak
16 Replies

8. UNIX for Dummies Questions & Answers

Quick question about set number

In my .exrc file I have line numbers turned on but it adds an indent. I don't like this, is there a way to have the line numbers at the left edge of my terminal instead of indented? Here's my .exrc 1 set ignorecase noslowopen report=0 autoindent showmatch showmode nu 2 set... (4 Replies)
Discussion started by: ebadamageplan
4 Replies

9. UNIX for Dummies Questions & Answers

question about printing number of lines in a file

as the title, I had try use "wc -l test.txt" but it give me "<many spaces> 384 test.txt" but the result I want is just "384" could any person can help me that? Thx:( (5 Replies)
Discussion started by: a8111978
5 Replies
Login or Register to Ask a Question