Visit Our UNIX and Linux User Community


unique random numbers awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting unique random numbers awk
# 1  
Old 07-16-2012
unique random numbers awk

Hi, I have a small piece of awk code (see below) that generates random numbers.
Code:
 
gawk -F"," 'BEGIN { srand(); for (i = 1; i <= 30; i++) printf("%s AM329_%04d\n",$0,int(36 * rand())+1) }' OFS=, AM329_hole_names.csv

The code works fine and generates alphanumeric numbers like AM329_0001, AM329_0009 etc etc. The problem is the numbers generated are not always unique. In other words I want a list of non-duplicate random numbers, no repeats. I came across some code and tried adapting it to my example. However, I can't seem to get it to work. My attempt is as follows:
Code:
 
gawk -F"," 'BEGIN { srand(); for (i = 1; i <= 30; i++) {do {select = printf("%s AM329_%04d\n",$0,int(36 * rand())+1)} while (select in pick) pick[select] = select} for (j in pick) printf("%s OR109_%04d\n", pick[j]) printf("\n")}' OFS=, AM329_hole_names.csv

Any help would be greatly appreciated.Smilie

Last edited by theflamingmoe; 07-16-2012 at 11:52 PM.. Reason: Small typo error
# 2  
Old 07-17-2012
Try this:

Code:
gawk -F"," 'BEGIN { srand(); for (i = 1; i <= 30; i++) { v=int(36 * rand()+1); if(v in p) i-- ; else { printf("%s AM329_%04d\n",$0,v); p[v] }}}' OFS=, AM329_hole_names.csv

# 3  
Old 07-17-2012
Thanks Chubler_XL, that worked like a charm Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #805
Difficulty: Medium
The goal of a Digital Signal Processor (DSP) is usually to measure, filter or compress discrete digital signals.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace all string matches in file with unique random number

Hello Take this file... Test01 Ref test Version 01 Test02 Ref test Version 02 Test66 Ref test Version 66 Test99 Ref test Version 99 I want to substitute every occurrence of Test{2} with a unique random number, so for example, if I was using sed, substitution would be something... (1 Reply)
Discussion started by: funkman
1 Replies

2. Homework & Coursework Questions

Random numbers

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! Write a shell script that will take the sum of two random number? Ex: Random n1 +Random n2 = result i tries to write it but i had some dufficulties ... (3 Replies)
Discussion started by: renegade755
3 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

Random float numbers in BASH

Hi people :) I'm learning shell scripting using bash and I want to generate 4 floating point number with 5 decimal places and write them to a file and a variable. I've done all this except the $RAMDOM enviroment variable does not generate a float number but a integrer. I hope you could... (3 Replies)
Discussion started by: pharaoh
3 Replies

5. Shell Programming and Scripting

Generating random numbers

Hi, I am having trouble with generating random numbers. can this be done with awk? So I have a file that looks like this: 23 30 24 40 26 34 So column1 is start and column2 is end. I want to generate 3 random #'s between start and stop: So the output will look like this: ... (9 Replies)
Discussion started by: phil_heath
9 Replies

6. Shell Programming and Scripting

Random Numbers - Perl

Hi Guys I have a script to find Ranomd numbers. But I want to make the file to produce more random. Could u guys help me plz. In this Script I have the code that generates random in for loop and the range I have specified in my %chromlength input and out put will be like this chrno start end... (3 Replies)
Discussion started by: repinementer
3 Replies

7. Shell Programming and Scripting

Random numbers from 0 to 1000

Hello All, I want to make a simple script which generate random number from 0 to 1000. and simply display it. Plz HELP!!!!!! Regards, Waqas Ahmed (2 Replies)
Discussion started by: wakhan
2 Replies

8. UNIX for Dummies Questions & Answers

how can i isolate the random sequence of numbers using awk?

as you can see there is a delimiter after c8 "::". Awk sees the rest as fields because it doesn't recognize spaces and tabs as delimiters. So i am basically looking to isolate 20030003ba13f6cc. Can anyone help? c8::20030003ba13f6cc disk connected configured unknown (2 Replies)
Discussion started by: rcon1
2 Replies

9. UNIX for Dummies Questions & Answers

Random numbers without repetition

Is anyone know some scripts to generate random number without repetition using bash; for example generate 10 different random numbers. Thanks (8 Replies)
Discussion started by: asal_email
8 Replies

Featured Tech Videos