Create random number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create random number
# 1  
Old 07-12-2012
Create random number

Hi,


I'm trying to create a script that will print random numbers with length of three.

Below is the expected out.

928-377-899
942-458-310
951-948-511
962-681-415
995-161-708
997-997-209

thanks
# 2  
Old 07-12-2012
this generates random number as u expected.. but i am not sure about its uniqueness. Below command gives you 7 results u can change the i for how much you want...

Code:
awk 'BEGIN { for (i = 1; i <= 7; i++) print ( int(1000 * rand()) "-" int(1000 * rand()) "-" int(1000 * rand())) }'

# 3  
Old 07-12-2012
You need to use srand() first..
# 4  
Old 07-12-2012
Perhaps seed the random number generator with srand() or you may get the same random numbers for each run...
Code:
$ awk 'BEGIN { for (i = 1; i <= 7; i++) print ( int(1000 * rand()) "-" int(1000 * rand()) "-" int(1000 * rand())) }'
237-291-845
152-585-193
810-173-484
151-366-491
910-265-893
220-631-571
332-104-502

$ awk 'BEGIN { for (i = 1; i <= 7; i++) print ( int(1000 * rand()) "-" int(1000 * rand()) "-" int(1000 * rand())) }'
237-291-845
152-585-193
810-173-484
151-366-491
910-265-893
220-631-571
332-104-502

$ awk 'BEGIN { for (i = 1; i <= 7; i++) print ( int(1000 * rand()) "-" int(1000 * rand()) "-" int(1000 * rand())) }'
237-291-845
152-585-193
810-173-484
151-366-491
910-265-893
220-631-571
332-104-502

$ awk 'BEGIN { srand(); for (i = 1; i <= 7; i++) print ( int(1000 * rand()) "-" int(1000 * rand()) "-" int(1000 * rand())) }'
676-830-414
258-178-379
76-885-517
508-900-613
718-789-553
880-689-744
142-8-667

$

# 5  
Old 07-12-2012
Code:
[root@node3 ~]# for i in {0..5}; do echo $((RANDOM%900+100))-$((RANDOM%900+100))-$((RANDOM%900+100)); done
305-222-530
832-784-382
460-325-680
598-111-820
314-888-186
640-554-449

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Create 'n' number random pairwise combination of words

File 1 contains the list of words that needed to be randomly paired: Tiger Cat Fish Frog Dog Mouse Elephant Monkey File 2 contains the pairs that should not be used (in any solution) during random pairing. Elephant-Dog Cat-Fish Monkey-Frog Dog-Elephant, Fish-Cat, Frog-Monkey... (1 Reply)
Discussion started by: sammy777888
1 Replies

2. Programming

random number

How can I choose randomly the row numbers of my file in awk? (4 Replies)
Discussion started by: Homa
4 Replies

3. Shell Programming and Scripting

Random pieces of number

Hello folks, i have number for example 10 and i want to divide into 4 random pieces that may be (6+2+1+1). How can i do this via script i have random number 234951 and i want to divide into 31 pieces. (6 Replies)
Discussion started by: learnbash
6 Replies

4. Programming

Random number is not reused

I need to generate and reuse a 5 digit random number every time my program is executed. But the following generates random numbers every time the function is called. #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include <time.h> ... (12 Replies)
Discussion started by: limmer
12 Replies

5. Programming

C Help; generating a random number.

Im new to C, and Im having a hard time getting a random number. In bash, I would do something similar to the following to get a random number; #!/bin/bash seed1=$RANDOM seed2=$RANDOM seed3=$RANDOM SEED=`expr $seed1 * $seed2 / $seed3` echo ${SEED%.*} Now, in online examples... (4 Replies)
Discussion started by: trey85stang
4 Replies

6. Shell Programming and Scripting

Regarding Random Number Genration

HI please help me in solving this issue. I have a shell script which consists of ten statemnts ( i mean 10 executable statments)...........and if i run that script the 10 statmenst will execute continously ............ But now my probelm is i have to write another script --> to... (0 Replies)
Discussion started by: lalitka
0 Replies

7. Programming

how to create random no between 10 to 40 in C

can any one tell me how to create integer random no between 10 to 40 in C language.When i m using random() or rand() functions they r creting some long int which is not required (5 Replies)
Discussion started by: useless79
5 Replies

8. Programming

Random number generation

Hi...How can I generate random numbers between a given range ...preferably between 1 and 100...in UNIX C programming...? I tried out functions like rand(),drand48() etc but didnt get satisfactory results... Thanks a lot in advance.......... (1 Reply)
Discussion started by: tej.buch
1 Replies

9. Shell Programming and Scripting

create a Random passwd

Hello Everyone, I am wondering how to create a Random Password of 8 characters for a user. It has to be run in a script. Also, The user have to be asked to change his/her password the first time they login. I tried using genPass but seems like my system doesn't support that command. For... (1 Reply)
Discussion started by: bashirpopal
1 Replies

10. Programming

How to generate a random number?

How to generate a random integer with specific range(for example, from 1 to 1000)? Also, how to convert a floating point number into a integer? (2 Replies)
Discussion started by: MacMonster
2 Replies
Login or Register to Ask a Question