Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello Experts

See my code:
C#
char[] elementArray = new char[] { 'A', 'B', 'C', 'D' };

char[] mainArray = new char[3000];

Random random;
for (int i = 0; i < 3000; i++)
{
    int seed = (int)DateTime.Now.Ticks;
    random = new Random(seed);
    mainArray[i] = elementArray[random.Next(4)];
}

This simple code generate my mainArray; But not exactly what I want.

Sample of Excepted results:
A B D A A C B C D A B B C A D A C C D A C D B B B B A C...

What I earn from the above code usually:
A C C C C C C C C C ... A A A A A A A ... D D D D D D ...


How can I earn the correct result?

If it is not clear let me know!
Posted
Updated 2-Dec-13 18:41pm
v2
Comments
Sergey Alexandrovich Kryukov 3-Dec-13 0:51am    
Not clear. The verb "earn" means making money or income, revenue, or other valuable things, such as trust...
—SA
Karthik_Mahalingam 3-Dec-13 0:53am    
ya... :)
Meysam Toluie 3-Dec-13 0:56am    
The English is not the first language of Many users and I. So take it easy.
Maybe I could use 'Get' or 'gain' or something else which I do not know!
Is there something else which is not clear?
Karthik_Mahalingam 3-Dec-13 1:12am    
sorry for that, sergey gave right answer
Sergey Alexandrovich Kryukov 3-Dec-13 8:55am    
You are talking as if I had written something unclear, not you... :-) No problem though.
Okay, I fixed you bug. Will you accept the formally now?
—SA

1 solution

Your mistake is very common: you need to create an instance of Random only once in the lifetime of your application domain, not in the loop, but before it. Move two first lines inside loop outside of the loop, before it.

—SA
 
Share this answer
 
Comments
Karthik_Mahalingam 3-Dec-13 1:11am    
you r right...
Sergey Alexandrovich Kryukov 9-Dec-13 2:13am    
Thank you, Karthik.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900