• http://www.techneurons.com/career/
  • experienced programming consultants for hire !!!

    Contact Now

    C#.NET Support

    Programming Question - Generate unique random numbers

    Posted by saba on Mar 25, 2010
    Write a program to generate list of 10000 random numbers. Numbers should be unique & between & 10000....
    The problem is i got the list of 10000 numbers but could not avoid redundancy...
    can u please hlep me out
    Replied by consultsarath on Mar 26, 2010
    <p>Here is a solution which uses&nbsp;a generic List object to generate random but unique series of numbers. This process is really time consuming for 10000 numbers. You can try this anyway. Please get back to me, if you face issues with this.</p> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">int</span>[] GenerateUniqueRandomNumbers(<span style="color: blue">int</span> count)</span></div> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></div> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Collections.Generic.<span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt; iList = <span style="color: blue">new</span> System.Collections.Generic.<span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt;();</span></div> <div style="line-height: normal">&nbsp;</div> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">while</span> (iList.Count &lt; count)</span></div> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></div> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Random</span> r = <span style="color: blue">new</span> <span style="color: #2b91af">Random</span>();</span></div> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> rand = (<span style="color: blue">int</span>)<span style="color: #2b91af">Math</span>.Floor(r.NextDouble() * count*10000) + 1;</span></div> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (!iList.Contains(rand))</span></div> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; iList.Add(rand);</span></div> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></div> <div style="line-height: normal">&nbsp;</div> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> iList.ToArray();</span></div> <div style="margin: 0cm 0cm 10pt"><span style="line-height: 115%; font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></div>
    We are experts in Cloud Computing Technologies. We can assist you to build high scalable business applications using Amazon Web Services (Amazon EC2, Amazon S3, Amazon SES, SNS, CloudFront), Windows Azure Platforms - Windows Azure and SQL Server Azure, Google App Engine using Python and Django Framework. We are Expert Programming Consultants available at affordable rates per hour. We work on several technologies - .NET, Python, Google App Engine, PHP, Windows Azure, Amazon Web Services ...