Manage Learn to apply best practices and optimize your operations.

Creating a random/unique number

Get a 4-digit random 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

Dig Deeper on Domino Resources - Part 3

  • Favorite iSeries cheat sheets

    Here you'll find a collection of valuable cheat sheets gathered from across the iSeries/ community. These cheat ...