943,758 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 949
  • C# RSS
Jan 14th, 2009
0

Same numbers in randomized array

Expand Post »
I have function which randomizes numbers:
C# Syntax (Toggle Plain Text)
  1. public int losuj()
  2. {
  3. Random losowanie = new Random();
  4. int los = losowanie.Next(0, 9);
  5. return los;
  6. }
I want to fill array with numbers generated by losuj function, so I wrote:
C# Syntax (Toggle Plain Text)
  1. for (int i = 0; i < 9; i++)
  2. for (int j = 0; j < 9; j++)
  3. planszaTab[i, j] = losuj();
Instead of array with random numbers all I get is array fill with the same number ex. 3. Each time I run program I get different number in which array is filled.

What I do wrong?
Last edited by kaczmar86; Jan 14th, 2009 at 6:24 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kaczmar86 is offline Offline
3 posts
since Jan 2009
Jan 14th, 2009
0

Re: Same numbers in randomized array

Because its happening so fast the seed is the same for all.
move the
C# Syntax (Toggle Plain Text)
  1. Random losowanie = new Random();
to outside the function so its not recreated each time, so its a private variable to your class. You should get different numbers then.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Jan 14th, 2009
0

Re: Same numbers in randomized array

You want random numbers in the range from 0 to 9. Which is 10 possible outcomes.
The array in the for loops goes from 0 to 8, which is 9 places.
If that is what you want, OK with me. But I like to mention it anyway.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: no intellisense ? why not.. :S
Next Thread in C# Forum Timeline: referencing an access database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC