| | |
Same numbers in randomized array
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jan 2009
Posts: 3
Reputation:
Solved Threads: 0
I have function which randomizes numbers:
I want to fill array with numbers generated by
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?
C# Syntax (Toggle Plain Text)
public int losuj() { Random losowanie = new Random(); int los = losowanie.Next(0, 9); return los; }
losuj function, so I wrote: C# Syntax (Toggle Plain Text)
for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) planszaTab[i, j] = losuj();
What I do wrong?
Last edited by kaczmar86; Jan 14th, 2009 at 6:24 am.
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
Because its happening so fast the seed is the same for all.
move the to outside the function so its not recreated each time, so its a private variable to your class. You should get different numbers then.
move the
C# Syntax (Toggle Plain Text)
Random losowanie = new Random();
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
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.
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
![]() |
Similar Threads
- Help Needed with GUI (Java)
- Help needed filling array with unique random numbers (C++)
- Random Parralel Array (C++)
- help with sorting program (C++)
Other Threads in the C# Forum
- Previous Thread: no intellisense ? why not.. :S
- Next Thread: referencing an access database
Views: 585 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# chat check checkbox class client code combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms ftp function gcd gdi+ http httpwebrequest image index input install java label list listbox listener login math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion prime programming radians regex remote remoting resource saving serialization server sleep socket sql statistics stream string tcp text textbox time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






