-2

I want to create a function that generates a random UK vehicle registration plate (using the structure introduced in 2001) but really don't know where to start.

All plates follow the structure:

[two letters][two digits][3 random letters]

e.g. BD51 SMR

More information on the structure can be found here: http://goo.gl/jh8Ldi

The first two letters represent an area code of which there are a few hundred and it's not as simple as A + A-Z, B + A-Z etc etc. For example A can then be followed by any letter except I, Q and Z, whereas D can only be followed by letters A-K (excluding i).

The two number represent the year of manufacture - I think I'm fine on this one.

The final three letters I can deal with.

Is there anyway of getting the first two letters without having to list all possible outcomes in an array?

Thanks

3
  • It might be possible to get the first two letters without an explicit list of values, but you'll need to tell us what the algorithm is for generating them.
    – Kryten
    Apr 28, 2014 at 17:12
  • I don't think there's another way to do this. You're always going to need to be able to check what you've got is a valid result, whether that's by a lookup or very complex rules in the code. Depending on what you're using this for - does it need to be truly random? It looks like a few of the initial letters let you have a second letter from A-Y, so if you're prepared to sacrifice some possibilities, you can make something that's less complicated, by just choosing from those.
    – andrewsi
    Apr 28, 2014 at 17:14
  • D Dees Shrewsbury L M N O P R S T U V W X Y Apr 28, 2014 at 17:14

2 Answers 2

1
Is there anyway of getting the first two letters without having to list all possible outcomes in an array?

Well, yes. You can encode the rules as shown in the Wikipedia tables - it's A-Z (with some exclusions) followed by A-Y (with different exclusions) but then some exceptions (e.g. X only has A-F following it).

But why not use an array? The wikipedia page lists them all out for you, it's only a few mins work to turn that into an array, and then you don't need to work out what the best way of encoding it is.

0

I think this text from the indicated "Vehicle Registration..." page is the key to answering your question:

In addition to the above local memory tags, personalised registrations are also offered
with arbitrary "local memory tags" prefixes, except for the letters I, Q, and Z.

If I read this correctly then the letter D can be followed by any letter except I, Q, and Z, if somebody requests (and is granted) such a local memory tag. In which case all you need to do in order to generate a random local memory tag is write a function f() which chooses a positive integer at random from 1 to 23, and then call it twice, mapping each number to a letter:

1->A
2->B
...
8->H    // skip I
9->J
10->K
...
15->P   // skip Q
16->R
17->S
...
23->Y   // skip Z

Not the answer you're looking for? Browse other questions tagged or ask your own question.