Page 1 of 1

Posted: Sat Dec 19, 2009 3:56 pm
by Malcolm
Your random number generator is a fucking abysmal piece of shit. Whoever let the Random class get out the door w\o reliable pseudorandom number generation needs to be drug out into the street & shot. It took me half a fucking day to find a method that works.



Edited By Malcolm on 1261256191

Posted: Sat Dec 19, 2009 4:02 pm
by GORDON
Pretty easy to make a "random" number generator based on the system clock, no matter what language you are using.

Posted: Sat Dec 19, 2009 4:25 pm
by Malcolm
GORDON wrote:Pretty easy to make a "random" number generator based on the system clock, no matter what language you are using.

Nope. Not here. Trying to fuck w\ the DateTime class was pointless as well. That's what the Random class does by default anyhow. & it fucks up horribly. Had to break a class out of the secured crypto service package that pseudorandomly generates some bytes.

EDIT : I was generating dozens of random numbers per 100 nanoseconds. It just wasn't cutting it. I'd've needed time down to picoseconds or something.




Edited By Malcolm on 1261258025

Posted: Sat Dec 19, 2009 5:27 pm
by TheCatt
Show me your code that wasn't working. Then, I'll tell you why it wasn't working.

Posted: Sat Dec 19, 2009 5:35 pm
by Malcolm
TheCatt wrote:Show me your code that wasn't working. Then, I'll tell you why it wasn't working.

I couldn't get random enough seeds. Tried ...

long longDiff = DateTime.Now.ToUniversalTime().Ticks -
new DateTime(1970, 1, 1).ToUniversalTime().Ticks()

Then forcibly cast it to an int, losing precision in the upper part, but maintaining randomness in the lower part.

int intDiff = (int)longDiff
Random rand = new Random(intDiff)

Then tried some trick w\ the Enumerable.Range() method, but that ended up being linear time, killing my run time overall.

Then tried this. Still not quick enough.

Finally found this. That worked.

Got my pseudorandomness going just fine now.




Edited By Malcolm on 1261262173

Posted: Sat Dec 19, 2009 5:47 pm
by GORDON
Without even looking at your code, you can't copy system_time_NOW into a work_time_variable, grab the last digit of the picosecond, and call that your randomly generated number? That's, like, 3 lines in RPG.

Posted: Sat Dec 19, 2009 6:41 pm
by Malcolm
From Macroshaft's own docs ...
The DateTime value type represents dates and times with values ranging from 12:00:00 midnight, January 1, 0001 Anno Domini (Common Era) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.)

Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar.

I am unable to get to a finer granularity than 100s of nanoseconds w\o some form of cheating.

Posted: Sat Dec 19, 2009 7:17 pm
by TheCatt
Are you seeding it every time you call it? You should make the instance of Random a static or other variable that you can continue to call, pulling out numbers, without reseeding, using Random.Next().

The whole point of the Random class is that you seed it once then call it multiple times to get a stream of numbers.

I just generated 20,000 random numbers in less than a second, which means nano-second level. It worked just fine.


Code: Select all


        private void getRandoms()
        {
            List<int> newList = new List<int>();
            Random r = new Random();
            DateTime Start;
            DateTime End;
            Start = DateTime.Now;
            for (int i = 0; i < 20000; i++)
            {
                newList.Add(r.Next(100));
          }
            End = DateTime.Now;
[/color]



Edited By TheCatt on 1261268331

Posted: Sat Dec 19, 2009 7:34 pm
by Malcolm
The dude I'm programming this shyte w\ supposedly had some info to the effect of ... after the first generation, C#'s Random class fails to provide decent randomness.

Arguing w\ him was even more of an uphill fight than finding a decent algorithm.

Posted: Sat Dec 19, 2009 7:40 pm
by TheCatt
What are you using the #s for? I mean, yea, it's not the best number generator in the world, but it's fine for anything but cryptography.

Posted: Sat Dec 19, 2009 8:14 pm
by Malcolm
TheCatt wrote:What are you using the #s for? I mean, yea, it's not the best number generator in the world, but it's fine for anything but cryptography.
It's not what I'm using them for that was causing issues ... alright, I guess I suppose it is. I'm essentially using a simulator to guess at weights in a mathematical system. I want shyte as "random" as possible so I can get the least tainted results.

Working on a game, need to assign proper costs for each stat. I've got a fight simulator that runs some sets of input 100x each. I bet I need a few hundred random #s per fight. Was running thousands of fights serially, lots of Randoms() getting created, all giving me identical results, skewing my results like a biazniatch. I really want these stats as closed to balanced as possible, so I'm being extremely picky about the generator.

Posted: Sat Dec 19, 2009 8:49 pm
by TheCatt
OK, so the fundamental issue is that you were instantiating lots of Randoms. Just make a single static reference for everything to use, and you're fine. The primary failing is that the generator is predictable, but it's still essentially "random"

Posted: Sat Dec 19, 2009 9:09 pm
by Malcolm
Yeah, I was making thousands of Randoms. Now I got a way to make random ints & doubles w\o having to worry about the shortcomings of C# designers.

Posted: Sat Dec 19, 2009 10:11 pm
by TheCatt
Well, it was there all along, you just took the long route. :)

At any rate, this isn't a fault of C#, it's a fault of random number generators. They expect you to code properly.

Posted: Sat Dec 19, 2009 10:12 pm
by GORDON
Zing!

Posted: Tue Dec 22, 2009 1:53 pm
by TheCatt

Posted: Tue Dec 22, 2009 11:39 pm
by Malcolm
TheCatt wrote:Here you go.
Damn, I actually want one now.

Posted: Wed Dec 30, 2009 3:19 am
by Malcolm
I'm going to echo the phrase that's been the death of many a designer ...

My combat system is pretty fucking balanced.

Posted: Thu Jan 07, 2010 8:26 pm
by Vince
Malcolm wrote:
TheCatt wrote:Here you go.
Damn, I actually want one now.
I have absolutely no need for it, and I want it too.