4
3
Scenario:
Imagine a situation where you wish to share a unique alphanumeric code. Perhaps it is a one-time-use coupon code you hand write on business cards, or some other scenario where it is desirable to have a code where mis-keying the entry is unlikely to produce valid result and the another valid code is not readily guessed.
However, when you are issuing these codes you do not have access to the list of codes that have already been issued. Rather, you know only how many have been issued before.
Challenge:
Write a function that generates a unique string of length n, subject to the following constraints:
- Available Characters: Each character in resultant string shall be from a configurable list of allowed values, which may be "hard-coded" into the algorithm.
- Sample List: A, B, C, D, E, F, G, H, J, K, L, M, N, P, Q, R, S, T, U, V, W, X, Z, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (A-Z and 0-9, omitting "I" and "J")
- Configurable Length Output: The length n of the generated string shall be configurable within the algorithm, for n<=8.
- Input: A seed is available (unique sequential positive integer >=1 and up to 10 digits)
- Stateless: The algorithm has no memory of previously generated strings (no database calls)
- Repeatable: The same seed shall yield the same result
- Unordered Output: Sequential seed values shall not generate strings in any obvious order (that is to say, results should superficially "appear" random)
- For example,
1:"AAAA", 2:"AAAB", 3:"AAAC", ...
would NOT qualify, while1:"RR8L", 2:"1D9R", 3:"TXP3", ...
is on the right track. - Note: While this criterion is admittedly subjective, "degree of randomness" is not intended to be the core aspect of this function. Given the Popularity Contest format, I'll leave it up to the community to decide whether a pattern is too obvious.
- For example,
- Unique Output: String shall be unique, at least within limitations of available characters and requested string length. Therefore, each possible permutation is to be used exactly once before any combination may be reused.
- For example, if configured to generate a string of length n=5 and the available characters include A-Z and 0-9 except for "O", "I" (34 possible characters), then the string may not repeat between seed>=1 and seed<=45,435,424.
- However, if length n=4 with 34 available characters, then the same string need only be unique on seed>=1 and seed<=1,336,336
Winning Criteria:
Popularity Contest - Submission with the most upvotes will be selected as the winner no sooner than two weeks after the first valid solution. Please upvote any and all submissions you feel adequately meet the above criteria.
1Welcome to PPCG. I'm afraid your very dry spec makes me think this is homework! If it is a genuine challenge, some pointers: 1. Why an algorithm and not a program/function? 2."Code challenge" means you must specify an objective criterion for the winning answer. Other tags eg "code golf" (shortest code) have implied winning criterions. 3. why not specify the allowable characters in the question? 4. what is the maximum length (and by extension what is the maximum seed value? do all possible output strings have to be generatable?) 5. It's v. similar to some other questions, it may be a duplicate – Level River St – 2015-02-13T21:48:44.903
Forgive me, while I have visited PPCG before, I'm not yet familiar with all of the guidelines and traditions here so I certainly appreciate your feedback. No, it is not homework. The idea came up when pondering how one might generate a unique record "lookup key" where other record locators such as barcodes, serial numbers, etc. While this could easily be achieved by querying a database of previously generated lookup keys, the idea of eliminating that option posed a unique challenge I thought others may find intriguing. I didn't find other questions quite like this one, so I posted it here. – Cragmonkey – 2015-02-14T01:04:07.107
how is it supposed to be stateless but avoid using previously used strings? – feersum – 2015-02-14T01:20:33.797
@feersum what he means by stateless is that it doesn't store previously used strings. I see no problem with that bit. I have a minor issue with "appear to be random" as this is highly open to interpretation. A simple substitution cipher might suffice, (though a more diligent entry might enhance it by having the key depend on the previous character.) – Level River St – 2015-02-14T01:31:32.370
That's the main point of the challenge - to devise a way to generate a unique string without knowing what came before. The results need not be random, but simply have that appearance by not occurring in any obvious order. – Cragmonkey – 2015-02-14T01:31:49.900
@cragmonkey the reason your question was put on hold is that it lacks a winning criterion. You can edit to add one (such as code golf) and it will go to the review queue. I would suggest you ask for a program/function, not an algorithm, and nail down the other particulars such as maximum seed value and word length. My general feeling was that encryption and random number generators have been done to death, but its true I can't find your exact question and a variable base (ie number of allowed characters) does rule out many seeded rng algorithms. I dont know how this question will be received. – Level River St – 2015-02-14T01:47:26.513
2
Either way, stick around and answer a few questions to find out what we're about. Writing a good challenge is hard. Feel free to post your next challenge in the sandbox for review http://meta.codegolf.stackexchange.com/q/2140/15599
– Level River St – 2015-02-14T01:50:17.217This now has a winning criterion, and I think it should be reopened. – KSFT – 2015-02-14T17:42:00.337
Is the seed the same as the number of strings issued before, or are they 2 separate inputs? – glebm – 2015-02-15T14:06:37.387
@glebm They're the same input. – Cragmonkey – 2015-02-15T16:37:21.290