Last post Sep 28, 2009 10:47 AM by johnly
Member
59 Points
78 Posts
Sep 26, 2009 03:15 PM|Kazuya_Mash|LINK
i Want to generate fix size 9 Digit Random Numbers....i am using this code the problem is some thime it return me 8
Length Digit Some time 9 or 7....I want exact 9 Every time
Public Function GenerateCode() As Long
Dim Code As Long
Code = GetRandomNumberInRange(12000000, 199999999)
Return CodeEnd Function
Public Function GetRandomNumberInRange(ByVal intLowerBound As Integer, ByVal intUpperBound As Integer) As Integer
Dim RandomGenerator As RandomDim intRandomNumber As Integer
RandomGenerator =intRandomNumber = RandomGenerator.Next(intLowerBound, intUpperBound)
GetRandomNumberInRange = intRandomNumber
New Random()
End Function
Star
10925 Points
2218 Posts
Sep 26, 2009 03:30 PM|Steelymar|LINK
try with this range:
Code = GetRandomNumberInRange(100000000, 999999999)
8270 Points
2195 Posts
Sep 26, 2009 03:43 PM|booler|LINK
The comment above contains the solution, but note you could simplify all of that down to a single line
Public Function GenerateCode() As Long Return New Random().Next(100000000, 999999999) End Function
All-Star
28988 Points
7251 Posts
Sep 26, 2009 03:47 PM|Rajneesh Verma|LINK
Hi,
You can try like this ............
protected void Page_Load(object sender, EventArgs e)
{
Random randnos = new Random();
string mpassword = RandomString(9, randnos);
}
private string RandomString(int size, Random random)
StringBuilder builder = new StringBuilder();
char ch;
while (builder.Length < 9)
int charCode = Convert.ToInt32(Math.Floor(18 * random.NextDouble() + 48));
if ((charCode >= 48 && charCode <= 57))
ch = Convert.ToChar(charCode);
builder.Append(ch);
return builder.ToString();
But this code is in c# you need to convert it in VB... yourself or use this tool
http://www.developerfusion.com/tools/convert/vb-to-csharp/
Thanks
Sep 26, 2009 04:28 PM|booler|LINK
Rajneesh Verma You can try like this ............
If you want a random string, one easy way to generate it is like this:
Public Function GenerateRandomString() As String Return Guid.NewGuid().ToString().Replace("-", String.Empty).Substring(0, 9) End Function
190 Points
53 Posts
Sep 28, 2009 10:47 AM|johnly|LINK
Try this code. This may be what you are looking for
Public Function GenerateRandomString() As String Randomize() GenerateRandomString = Format(Int(999999999 * Rnd()), "000000000") End Function
Please mark as 'Answer' if it helps
Member
59 Points
78 Posts
Fixed Length Random Number
Sep 26, 2009 03:15 PM|Kazuya_Mash|LINK
i Want to generate fix size 9 Digit Random Numbers....i am using this code the problem is some thime it return me 8
Length Digit Some time 9 or 7....I want exact 9 Every time
Public Function GenerateCode() As Long
Dim Code As Long
Code = GetRandomNumberInRange(12000000, 199999999)
Return CodeEnd Function
Public Function GetRandomNumberInRange(ByVal intLowerBound As Integer, ByVal intUpperBound As Integer) As Integer
Dim RandomGenerator As RandomDim intRandomNumber As Integer
RandomGenerator =intRandomNumber = RandomGenerator.Next(intLowerBound, intUpperBound)
GetRandomNumberInRange = intRandomNumber
New Random()
End Function
Star
10925 Points
2218 Posts
Re: Fixed Length Random Number
Sep 26, 2009 03:30 PM|Steelymar|LINK
try with this range:
Code = GetRandomNumberInRange(100000000, 999999999)
Star
8270 Points
2195 Posts
Re: Fixed Length Random Number
Sep 26, 2009 03:43 PM|booler|LINK
The comment above contains the solution, but note you could simplify all of that down to a single line
:
All-Star
28988 Points
7251 Posts
Re: Fixed Length Random Number
Sep 26, 2009 03:47 PM|Rajneesh Verma|LINK
Hi,
You can try like this ............
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">protected void Page_Load(object sender, EventArgs e)</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> Random randnos = new Random();</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> string mpassword = RandomString(9, randnos);</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> private string RandomString(int size, Random random)</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> StringBuilder builder = new StringBuilder();</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> char ch;</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> while (builder.Length < 9)</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> int charCode = Convert.ToInt32(Math.Floor(18 * random.NextDouble() + 48));</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> if ((charCode >= 48 && charCode <= 57))</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> ch = Convert.ToChar(charCode);</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> builder.Append(ch);</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> return builder.ToString();</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div>protected void Page_Load(object sender, EventArgs e)
{
Random randnos = new Random();
string mpassword = RandomString(9, randnos);
}
private string RandomString(int size, Random random)
{
StringBuilder builder = new StringBuilder();
char ch;
while (builder.Length < 9)
{
int charCode = Convert.ToInt32(Math.Floor(18 * random.NextDouble() + 48));
if ((charCode >= 48 && charCode <= 57))
{
ch = Convert.ToChar(charCode);
builder.Append(ch);
}
}
return builder.ToString();
}
But this code is in c# you need to convert it in VB... yourself or use this tool
http://www.developerfusion.com/tools/convert/vb-to-csharp/
Thanks
www.rajneeshverma.com
Star
8270 Points
2195 Posts
Re: Fixed Length Random Number
Sep 26, 2009 04:28 PM|booler|LINK
If you want a random string, one easy way to generate it is like this:
But I think the poster is after a number in this case.
Member
190 Points
53 Posts
Re: Fixed Length Random Number
Sep 28, 2009 10:47 AM|johnly|LINK
Try this code. This may be what you are looking for
Please mark as 'Answer' if it helps
Zebralive | Send BulkSMS