Tip

Creating a random/unique number

Here was my need. I needed a 4 digit number, randomly assigned, but once assigned could not be equal to any number previously assigned. Originally, I also had the option for the user to choose how many numbers they needed, and with that I just added another check prior to going to the view to see if the number had been assigned. I ran some insane checks on how long this took...tried asking for 500 numbers within a range of only 1000 available, and it took almost a minute. But if this was the requirement everything would have been done differently.

I have this in a script library. Looking for a random number between 1000 and 9999, not yet assigned.


 Function uniqueRandom Dim w As NotesUIWorkspace Dim s As NotesSession Dim db As NotesDatabase Dim view As NotesView Dim getdoc As NotesDocument Dim tmp As Variant Dim tmp2 As String Dim curDoc As NotesDocument Set w = New NotesUIWorkspace Set s = New NotesSession Set db = s.CurrentDatabase Set view = db.GetView("(SavedNumbers)") Set curDoc = w.CurrentDocument.Document uniqueRandom = "" retry: tmp = Evaluate ("@Integer( (9999 - 1000 )*@Random + 1000)",curdoc) tmp2$ = Cstr(tmp(0)) Set getdoc = view.GetDocumentByKey(tmp2$,True) If Not getdoc Is Nothing Then Goto retry End If uniqueRandom = tmp2$ End Function

This was first published in September 2001

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.