Question

  Asked: May 18 2008   6:44 PM GMT
  Asked by: Coolgirl04


Random number generation


SQL Server, SQL, Random Number Generator, .

Hi, I have a 6 digit number. I need to make this as a 10 characters with those 6 digits within it.
For eg, if my input number is 123456, the result should be a4y2d365p1. some combination of characters with the original 6 digit numbers.
Please do let me know if this could be done by any ways..

Subscribe to Alerts! Get questions and answers delivered to your Inbox.


E-mail me updates on this question



   SUBSCRIBE

hidden modal window

Answer Wiki (Improve, edit or add to this answer)


 RATE THIS ANSWER
0
Click to Vote:
  •   0
  •  0



Hello,
hereafter an example code:


Dim numbers
Dim letters
Dim ID
Randomize
numbers="0123456789"
letters="abcdefghijklmnopqrstuvwxyz"
for i=1 to 10
if i mod 2 <> 0 Then
ID = ID & mid(letters,Int((26*Rnd) +1),1)
Else
ID = ID & mid(numbers,Int((10*Rnd) +1),1)
end If
Next
wscript.echo ID
  • AddThis Social Bookmark Button

Browse more Questions and Answers on SQL Server and Development.

Looking for relevant SQL Server Whitepapers? Visit the SearchSQLServer.com Research Library.


Discuss This Answer


You must be logged-in to discuss a question. Log-in/Register

Willmage  |   May 19 2008  5:29PM GMT

The above answer will give you a random letter/number output, but it doesn’t take into account the original number the user started with. Here is an answer I have for this question:

Dim Letters, OrigNum
Dim TextNum, ID, TempHold
Dim RandNum, FinalAns

Letters = “abcdefghijklmnopqrstuvwxyz”
OrigNum = 123456

Randomize
Rem ** the following randomizes the original number
RandNum = “”
TextNum = Trim(Str(OrigNum))
For i = 1 To 5
ID = Int((Len(TextNum) * Rnd) + 1)
RandNum = RandNum & Mid(TextNum, ID, 1)
TempHold = Left(TextNum, ID - 1)
TempHold = TempHold & Mid(TextNum, ID + 1, Len(TextNum) - ID)
TextNum = TempHold
Next
RandNum = RandNum & TextNum
Rem ** the following creates your new random character/number string
FinalAns = “”
For i = 1 To 10
If Len(FinalAns) + Len(RandNum) = 10 Then
FinalAns = FinalAns & Left(RandNum, 1)
RandNum = Right(RandNum, Len(RandNum) - 1)
Else
If Len(RandNum) = 0 Then
FinalAns = FinalAns & Mid(Letters, Int(26 * Rnd) + 1, 1)
Else
If ((Int(Rnd * 2) + 1) Mod 2) <> 0 Then
FinalAns = FinalAns & Left(RandNum, 1)
RandNum = Right(RandNum, Len(RandNum) - 1)
Else
FinalAns = FinalAns & Mid(Letters, Int(26 * Rnd) + 1, 1)
End If
End If
End If
Next

Hope that helps!

 

Mrdenny  |   May 20 2008  12:30AM GMT

I’m curious, what’s the purpose behind this code?

Check out my SQL Server blog “SQL Server with Mr Denny” for more SQL Server information.