Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

working with rand function in c#. when placing rand in for loop i'm not able to generate a set of random numbers instead i'm getting a series of numbers like 01234..

$
0
0
         

i want to generate a set of random numbers and store that in an array, and when i was trying to do the same thing using a for loop i am getting a series of numbers like 1,1,1,1,.. or 6,6,6,6.. the same number is getting repeated. don't know why.

 i am having a string in n1 i.e.., string n1="helloadithyahowareyou"

and the variable randomNumber contains a value in between (0,n1.lenght) and for that i have written a separate code.. and that is working fine.

code:

          for (int i = 0; i <randomNumber; i++)
            {
                Random rand = new Random();
                int randnum = rand.Next(0, n1.Length);
                arr1[i] = randnum;
            }


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>