We are excited to announce that the ASP.NET Forums are moving to the new Microsoft Q&A experience. Learn more >

5 replies

Last post Sep 28, 2009 10:47 AM by johnly

• Kazuya_Mash

Member

59 Points

78 Posts

Fixed Length Random Number

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

• Steelymar

Star

10925 Points

2218 Posts

Re: Fixed Length Random Number

try with this range:

Code = GetRandomNumberInRange(100000000, 999999999)

• booler

Star

8270 Points

2195 Posts

Re: Fixed Length Random Number

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

:

• Rajneesh Ver...

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();

}

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

• booler

Star

8270 Points

2195 Posts

Re: Fixed Length Random Number

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

But I think the poster is after a number in this case.

• johnly

Member

190 Points

53 Posts