Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to add a random number to my documents as a unique identifier. How would I go about doing this? A Random number field code or something?

To clarify: I want a random number in my document generated automatically, without any dependencies, so that I could send the template file to others if need be.

share|improve this question
    
Should it be fixed once generated? Or should it change when saving, printing etcetera? –  Arjan Feb 16 '11 at 8:00
    
Do you want a random number, or do you want a unique identifier? The two are mutually exclusive. –  Dour High Arch Feb 16 '11 at 17:23

3 Answers 3

The suggestion for GUIDs is a good one. GUIDs are quite long, though.

If you want a random number that is smaller, you need to suggest about how many digits you want.

Generation of random numbers is not trivial, and most algorithms you will find are complete rubbish. Ideally you should have a random number generator with a uniform distribution. These tend to be few and far between.

If this is what you want, I'd suggest an M-sequence generator because they are extremely well understood and have a very nice uniform distribution. (They are also highly predictable, meaning that by knowledge of the current number and the algorithm you can easily get the number number - something common to all random number generators. However by taking a small sequence of the numbers generated you can reconstruct the generating function. This is not a big deal in many applications but is something to be aware of.)

share|improve this answer
    
And how would one do this in Word? –  Arjan Feb 16 '11 at 7:58
    
In word, you can do this using the algorithm in VBA. The algorithm for an M-Sequence is very easy, provided VBA allows bitwise shift, OR, AND and XOR operations. Its about 10 lines of code. The secret sauce is the magical M-sequence generating function. –  quickly_now Feb 16 '11 at 9:22
    
I would note however the the original question was about adding a UNIQUE IDENTIFIER to word documents. The thing with this is - is this PER DOCUMENT? Or PER RECIPIENT OF A DOCUMENT? This makes a big difference to what should be done. –  quickly_now Feb 16 '11 at 9:24

In what context? Is this a consistent thing required across multiple documents?

Why not just use GUIDs? There is a number of GUID generators around that could satisfy your need for random numbers.

It's not clear what you need without more information.

share|improve this answer
    
The OP wants to generate a random number in Word. –  Arjan Feb 16 '11 at 7:59
    
Entirely feasible if you connect a field to a random number generator in an Access DB for instance. It's not clear enough from the question though. –  user3463 Feb 16 '11 at 8:03

If you fire up a CMD prompt, you can type...

echo %random% | clip

...to get a random number between 0 and 32767 into the clipboard.

If you need a longer number, in PowerShell, you can type...

Get-Random -Minimum 9999 -Maximum 99999999 | clip

...to get a random number between any numbers you choose, into the clipboard.

I have not looked at this closely, but by using this method, you should be able to make a PowerShell script that will input the random number into Word automatically.

But, it's probably best to do it with a VBA script entirely within Word, but I don't know much about that.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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