Language:
Welcome Guest Login or Signup » LOGOUT

Go Back   LDS Mormon Forums > LDS.NET Popular Forums > Advice Board
You are not logged into the site. Please login or signup.

Notices

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-17-2008, 08:37 AM
unixknight's Avatar
Senior Member
 
Join Date: Jul 2008
Location: United States -
Posts: 185
Thanks: 50
Thanked 80 Times in 42 Posts
Laughs: 0
Got Laughs 0 Times in 0 Posts
Default Need help from a fellow programmer!

So I wanted to be able to determine configurations and loadouts for my units and experiment with different enemies, so I wrote a program to simulate hand to hand fights between units in Warhammer.

Alright so my program is pretty much done, but there seems to be a problem with the random number generator I'm using in my code. I've thoroughly checked the program and can find no logic errors, yet when I run the following simulation:

Bretonnian MAAs vs. VC Skeletons

The Bretonnians (Assuming a nearby knight) will refuse to charge (fail leadership) 70% of the time when the Skeletons are wielding spears and 85% of the time when the Skeletons have a hand weapon...

And yet the Leadership test has nothing whatsoever to do with the defending unit's weapon choice...

I think what's happening is the cheesy random number generator that comes with .NET is somehow being influenced by this. Either that, or somehow I'm missing something in code.

Anybody have any ideas on where I can get a decent randomizer?
__________________
A knight is sworn to valour. His heart knows only virtue. His blade defends the helpless. His might upholds the weak. His word speaks only truth. His wrath undoes the wicked.
-The Old Code
Reply With Quote
  #2 (permalink)  
Old 07-17-2008, 09:32 AM
unixknight's Avatar
Senior Member
 
Join Date: Jul 2008
Location: United States -
Posts: 185
Thanks: 50
Thanked 80 Times in 42 Posts
Laughs: 0
Got Laughs 0 Times in 0 Posts
Default

Alright so predictably, when I put in a fixed seed value it gave me 100% charger wins results.

So when I went back and set a Thread.Sleep(1) in the correct function (gawd)

I now have consistent 79% failure to charge results in both cases of defender weapons.

So it would seem that this pseudo random number generator blows (we knew that) and that the temporary fix is to cripple the program's performance by making it pause for a millisecond every time it rolls dice, which could happen several dozen times in each battle, and I like to run at least a thousand to get a decent statistical average.

So I am still on the lookout for a nicer randomizer to put in my Dice class, but at least this eliminates the rest of the code as being the source of the problem.
__________________
A knight is sworn to valour. His heart knows only virtue. His blade defends the helpless. His might upholds the weak. His word speaks only truth. His wrath undoes the wicked.
-The Old Code
Reply With Quote
  #3 (permalink)  
Old 07-20-2008, 06:53 PM
MarginOfError's Avatar
Senior Member
 
Join Date: May 2008
Location: United States -
Age: 27
Posts: 1,424
Thanks: 40
Thanked 661 Times in 361 Posts
Laughs: 15
Got Laughs 198 Times in 89 Posts
Default

Quote:
So it would seem that this pseudo random number generator blows (we knew that) and that the temporary fix is to cripple the program's performance by making it pause for a millisecond every time it rolls dice, which could happen several dozen times in each battle, and I like to run at least a thousand to get a decent statistical average.
Now I'm confused. If you have a pseudo random number generator (PRNG), why are you making the program pause between rolls? Are you switching from the default PRNG to a system based RNG (one that takes a random number based on, say, eletrical current at the time of the function call?

My guess is you're right. The RNG in .NET is probably kind of crappy. Could you import a call from C+, or some other language that has a more robust PRNG?

Also, if you want to look at whether or not the RNG is behaving randomly, I would suggest 10,000 calls...just my opinion.
Reply With Quote
  #4 (permalink)  
Old 07-20-2008, 09:47 PM
unixknight's Avatar
Senior Member
 
Join Date: Jul 2008
Location: United States -
Posts: 185
Thanks: 50
Thanked 80 Times in 42 Posts
Laughs: 0
Got Laughs 0 Times in 0 Posts
Default

Well I've got 2 paths to overall programming improvement.

The first is a much better random number generator that's high enough quality for encryption. I'm going to use that in my Dice class.

Second, instead of creating a new random object every time I roll the dice, I'm going to create it once and persist it across the various calls in the program, so that only the initial seed value is needed and everything after that will simply be next() calls on the object.

Basically my initial Dice class design was weak because I wrote it as a way to acclimate myself to VB.NET. (Previously I worked in C#.) I kinda wrote it on the side to get used to the syntax. At the time I wasn't too worried about the overall design.
__________________
A knight is sworn to valour. His heart knows only virtue. His blade defends the helpless. His might upholds the weak. His word speaks only truth. His wrath undoes the wicked.
-The Old Code
Reply With Quote
  #5 (permalink)  
Old 07-29-2008, 12:20 PM
Senior Member
 
Join Date: Feb 2008
Location: United States -
Posts: 959
Thanks: 164
Thanked 297 Times in 177 Posts
Laughs: 9
Got Laughs 28 Times in 14 Posts
Default

Sorry, I wish I had seen this thread earlier since you probably already made a work around, but I don't often browse the advice forum. I believe the problem you are running in to is that the .NET random number generator is seeded with the current time and seeds when you create the Random object. It works best when you only create one instance of the Random object and reuse that.

For example, in this case the random number generated will most likely be the same each time:
for (int i =0; i < 100; i++)
{
Random r = new Random();
int random number = r.Next(0, 1000);
}
But in this case, the random number will be different:
Random r = new Random();
for (int i =0; i < 100; i++)
{
int random number = r.Next(0, 1000);
}
If you make the random object as a static field and only instantiate it once it should fix this. The .NET random number generate does not suck, it just has to be used properly

If you have any other .NET type questions let me know.

Edit: just noticed you already noticed this a couple posts up

Last edited by DigitalShadow; 07-29-2008 at 12:22 PM.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

New Posts


All times are GMT -6. The time now is 12:12 AM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0



TERMS & CONDITIONS | HELP | CONTACT US | INVITE | RSS FEEDS | ABOUT US | GET INVOLVED | ARCHIVE
*** LDS Mormon Network ***
More Good Foundation. All rights reserved.

Header art used by permission of Mark Mabry and Reflections of Christ.

LDS.Net is not owned by or affiliated with The Church of Jesus Christ of Latter-day Saints (sometimes called the Mormon Church or LDS Church). The views expressed herein do not necessarily represent the position of the Church. The views expressed by individual users are the responsibility of those users and do not necessarily represent the position of the More Good Foundation. For the official Church websites, please visit LDS.org and Mormon.org.