cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
The Discovery Summit 2025 Call for Content is open! Submit an abstract today to present at our premier analytics conference.
Choose Language Hide Translation Bar
View Original Published Thread

Generating Random Letters

B1234
Level III

Hi, 

I have a script that opens data set where I have a list of "Operator". I have to manually assign each person a random letter in the "Analyst"  but wondering if there is a  code that could loop though all the different operators and assign a random letter to the analyst column?

B1234_0-1653378889551.png

 

Thanks!

2 ACCEPTED SOLUTIONS

Accepted Solutions
ian_jmp
Level X


Re: Generating Random Letters

Here's one way. Note that 'RandomShuffle()' assures you don't assign the same letter more than once (which I assume is what you need):

NamesDefaultToHere(1);

// List of possible values
letterList = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

// Number of rows in data table
n = 10;

// Values to assign to new character column
colVals = letterList[RandomShuffle(1::n)];

Print(colVals);

 

View solution in original post

jthi
Super User


Re: Generating Random Letters

One option would be to use associative array in combination with @ian_jmp solution. Something like this could work:

Names Default To Here(1);

dt = New Table("test",
	addrows(100),
	New Column("Operator", character, set values({"ABC", "ABC", "DEF", "GHI"}));
);

// List of possible values
letterList = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

uniq_oper = Associative Array(dt:Operator) << get keys;
Remove From(uniq_oper, Contains(uniq_oper, "")); // remove missing
uniq_oper_letter = letterList[Random Shuffle(1::N Items(letterList))][1::N Items(uniq_oper)];

oper_aa = Associative Array(uniq_oper, uniq_oper_letter);

dt << New Column("Analyst", character, << Set Each Value(
	If(Contains(oper_aa, :Operator),
		oper_aa[:Operator]
	,
		""
	)
));
-Jarmo

View solution in original post

4 REPLIES 4
ian_jmp
Level X


Re: Generating Random Letters

Here's one way. Note that 'RandomShuffle()' assures you don't assign the same letter more than once (which I assume is what you need):

NamesDefaultToHere(1);

// List of possible values
letterList = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

// Number of rows in data table
n = 10;

// Values to assign to new character column
colVals = letterList[RandomShuffle(1::n)];

Print(colVals);

 

B1234
Level III


Re: Generating Random Letters

Thanks, how do I account for the same operator name. I would have multiple rows where initials are "ABC" so I want all of these to be the same random letter?

 

NamesDefaultToHere(1);

dt=new table();
dt<<addrows(100);
dt<<new column("Operator", character, set values ({"ABC", "ABC", "DEF", "GHI"}));


// List of possible values
letterList = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

// Number of rows in data table
n = 10;

// Values to assign to new character column
colVals = letterList[RandomShuffle(1::n)];

Print(colVals);

dt<<new column ("Analyst", character,set values(colVals));

jthi
Super User


Re: Generating Random Letters

One option would be to use associative array in combination with @ian_jmp solution. Something like this could work:

Names Default To Here(1);

dt = New Table("test",
	addrows(100),
	New Column("Operator", character, set values({"ABC", "ABC", "DEF", "GHI"}));
);

// List of possible values
letterList = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

uniq_oper = Associative Array(dt:Operator) << get keys;
Remove From(uniq_oper, Contains(uniq_oper, "")); // remove missing
uniq_oper_letter = letterList[Random Shuffle(1::N Items(letterList))][1::N Items(uniq_oper)];

oper_aa = Associative Array(uniq_oper, uniq_oper_letter);

dt << New Column("Analyst", character, << Set Each Value(
	If(Contains(oper_aa, :Operator),
		oper_aa[:Operator]
	,
		""
	)
));
-Jarmo
B1234
Level III


Re: Generating Random Letters

@ian_jmp and @jthi  you both make this seem so easy! thank you both for your help

Recommended Articles

No recommendations found