aspose file tools*
The moose likes Java in General and the fly likes Unique 8-length chars algorithm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Unique 8-length chars algorithm" Watch "Unique 8-length chars algorithm" New topic
Author

Unique 8-length chars algorithm

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Hi.
I want to create an algorithm that create a unique 8-length charaters String, like :
ADIGJEZC, ERLOCBDO .....
Any ideas ?
Thanks for help.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Unique in what context?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
steven gerrard
Ranch Hand

Joined: Jan 21, 2006
Posts: 55
similar q was recently asked in Microsft code 4 bills contest held in india

the q there was to find the minimum length of string that makes all permutations of first 9 letters of english alphabet(taking 9 at a time) : a,b,c,d,e,f,g,h,i

eg abbaa is the minimum length of string that makes all possible pairs of alphabets(a,b) taking 2 at a time(aa,ab,ba,bb)

i thought a lot a lot abt this q but couldnt get the soln
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Unique in what context?

When ever I run the algorithm, I have to get a new String.
(Assume this algorithm is used to create PKs).
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Originally posted by John Todd:
Hi.
I want to create an algorithm that create a unique 8-length charaters String, like :
ADIGJEZC, ERLOCBDO .....
Any ideas ?
Thanks for help.


The correct answer: the task is impossible.

If you want to pose an answerable question it might be worded as follows: How does one generate the set of all 8 character permutations using upper case letters? I think that is what you meant to ask.

If you have taken an introductory statistics class you may recall this is simply a permutation. In permutations the position has meaning. AB is not the same as BA for example. Now, you have 26 objects (upper case letters) and 8 positions so this is just 26P8 (read: 26 permute 8).

Possible solutions: 26! / (26 - 8)! = 62990928000

So what you need is an algorithm to cycle through all of these possibilities. The way you do that is google for "generate permutations" I'ts probably not pretty.
[ February 18, 2006: Message edited by: Rick O'Shay ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18656
    
    8

Originally posted by John Todd:
When ever I run the algorithm, I have to get a new String.
(Assume this algorithm is used to create PKs).
There are hints in the question that you want the string you get from this algorithm to be different from some other set of strings. There are also hints that a database is involved. But the question "Unique in what context?" still hasn't been answered.

Databases already have built-in ways of generating unique primary keys. So if there's a database involved you might want to consider those ways instead of inventing your own.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

@Rick: He showed an example containing two Os, so he isn't going for permutations, so we have simply 26^8 solutions (=208 827 064 576).

@John:
After 26^8 cases, you may not get a unique String again.
To get a probably random string, you could use Java.util.Random.nextLong (), and to encode it, use the modulo-Operator.

Perhaps it's more easy to generate the encoded String while producing it character by character.

For few values, probably unique values might be unique enough - for a once running application, you could store used values in a Set.
For persistence a database or file might be appropriate.


http://home.arcor.de/hirnstrom/bewerbung
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
how would you do it with numbers ? how do you generate "unique" 8 digit numbers ?


00000000
00000001
....
00000009
00000010
00000011

there is no difference whether you use numbers or letters (we are just used to count in the decimal system.... but you could but imagine the alphabet as numbers from 0-26)

AAAAAAAA
AAAAAAAB
.....
AAAAAAAZ
AAAAAABA
AAAAAABB


to me this looks like two loops, one for the position and one for the character. some pseudocode:

if you need to stop/recover this sequence generation, then you need to memorize the current key and the position you are going to "increase".

something like this could generate keys... they are ordered (by knowing the last two, you can guess the next and by knowing just one you have a chance of 1/8 of guessing the next.

why do you want to do it like this ?
and as already said in one of the replies: your DB is probably better at generating keys...


pascal
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
>> He showed an example containing two Os..

Assuming that was intentional then what he is asking for (appears to have left the building) is nothing more than a counter from 00000000 to ZZZZZZZZ. May be base 26 but that hardly complicates matters.

Maybe he wants a random string with no duplicates of 8 characters in length? That being the case he can just keep a list of "numbers" already dished out, keeping them sorted for a fast binary search.

I noticed folks at The Ranch often fail to distill the germ of their question and leave out critical details. So you get "How do I display a pink polka dot on Tuedsay" instead of "How do I display an image contained in a JPEG file".
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18656
    
    8

Originally posted by Rick O'Shay:
I noticed folks at The Ranch often fail to distill the germ of their question and leave out critical details.
There's also a tendency for people to ask the second question. I.e.

"Okay, I need to have a unique key for my Widget table. How can I do that?"

"Well, I will write some code to generate one. Probably 8 characters will be enough. But what code to write to ensure it's unique? Let's ask on the ranch."

If this was the thought process, then it would have been much easier to answer the first question.
Jherald Lacambra
Ranch Hand

Joined: Feb 02, 2005
Posts: 129
here's a javascript that i have created that generates 8 characters string. Just convert the methods into java

var num = Math.random()+"";
num = num.substring(2,18)
var randomText = new Array("1" , "2", "3", "4", "5", "6", "7", "8", "9", "0",
"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", "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")

var indexNum = new Array( num.substring(0,2),
num.substring(2,4),
num.substring(4,6),
num.substring(6,8),
num.substring(8,10),
num.substring(10,12),
num.substring(12,14),
num.substring(14,16))
var numbers = indexNum[0] + "\n" + indexNum[1] + "\n" + indexNum[2] + "\n" + indexNum[3] + "\n" + indexNum[4] + "\n" + indexNum[5] + "\n" + indexNum[6] + "\n" + indexNum[7] + "\n";
var generatedString = "";
function randomizer(){
for(var i=0;i<8;i++){
if(indexNum[i]>62){
indexNum[i]=indexNum[i]-62;
generatedString = generatedString + randomText[parseInt(indexNum[i])];
}else{
generatedString = generatedString + randomText[parseInt(indexNum[i])];
}

}
document.registration1.genString.value = generatedString;
numbers = num +"\n" + numbers + generatedString

}


jherald
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Originally posted by Jherald Lacambra:
here's a javascript that i have created that generates 8 characters string. Just convert the methods into java...

}


Yikes! Why would you ever use an array to store sequential values when you can use the + operator? Anyway, if he wanted a random string (rather than unique strings as he asked for) he could generate that with a loop.



Remember that characters are people too, er... numbers.
Jherald Lacambra
Ranch Hand

Joined: Feb 02, 2005
Posts: 129
Originally posted by Rick O'Shay:


Remember that characters are people too, er... numbers.


YEAh that is my code. what if dont want it sequential? what if i don't want letters E,d,f, can you say you can use for loop for that?.. i've only created a static array so that i can define the letters or characters i want for future use.. The random string will be generated and can be check from the existing list of strings. if not then you have a unique string.
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Yes, you can change the letters but for what purpose? Let's leave aside efficiency and bloat issues and ask what the actual task is and what's the simplest way that we can achieve that in a reasonably extensible way.

The idea is to generate a string of N random characters withing a range R. That little loop will do that. There is nothing sequential about the result. It's essentially random. What purpose would it serve to choose your own non sequential characters if you are going to pick them randomly?

BTW, what makes you think you could not filter out E, f, d in a loop? A minor tweak to the loop is all that is required. KISS!!! Keep It Simple Students!
[ February 21, 2006: Message edited by: Rick O'Shay ]
Jherald Lacambra
Ranch Hand

Joined: Feb 02, 2005
Posts: 129
Originally posted by Rick O'Shay:
[QB]

BTW, what makes you think you could not filter out E, f, d in a loop? A minor tweak to the loop is all that is required. KISS!!! Keep It Simple Students!

for me its still simplier just to remove n(number of)characters to be randomize that having to modify codes? do you think so? how about removing 5 characters, ill just remove them from array the code is still intact, if i add 10 characters do i have to modify the codes? ill just modify the array..

Yes, you can change the letters but for what purpose? Let's leave aside efficiency and bloat issues and ask what the actual task is and what's the simplest way that we can achieve that in a reasonably extensible way.

The idea is to generate a string of N random characters withing a range R. That little loop will do that. There is nothing sequential about the result. It's essentially random. What purpose would it serve to choose your own non sequential characters if you are going to pick them randomly?



may randomizing the letters will not be an answer coz the will be 208827064576 possible combinations of letters will be formed.. if we will randomly create the string, the string might not be unique although there are alot of combinations.. maybe its better to do it sequentially based the number of characters

letters say A is 0,B is 1,C is 2,....


if you will make A as 0 then the first string will be 'AAAAAAAA' or 0000000000000000 the if you increment the last 0 by 1 it would be 0000000000000001 or 'AAAAAAAB' then strings will be unique to each other. until you reach 2626262626262626 or 'ZZZZZZZZ'
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
For sequential you would just count as I already pointed out. There are R to the N values or 36 to the power of 8 if you use 8 characters in the range of zero to capital 'Z'. Count 'em.

What I am trying to convey is that you have a lot of data and code for something that could be done in a loop. The future requirements you've mentioned seem somewhat contrived but, regardless, they are easily done using small tweaks to the loop. 'Nuff said.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18656
    
    8

I'm somewhere in the middle. I used the loop to generate the random characters and I also used the arrays (one for source characters, which exclude a few letters, and the other to store the result). I didn't need to use the + operator to build my string.
[ February 22, 2006: Message edited by: Paul Clapham ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unique 8-length chars algorithm