Hey, C#

Post Reply
Malcolm
Posts: 32040
Joined: Fri May 21, 2004 1:04 pm
Location: Minneapolis

Post 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
Diogenes of Sinope: "It is not that I am mad, it is only that my head is different from yours."
Arnold Judas Rimmer, BSC, SSC: "Better dead than smeg."
GORDON
Site Admin
Posts: 56735
Joined: Sun Jun 06, 2004 10:43 pm
Location: DTManistan
Contact:

Post by GORDON »

Pretty easy to make a "random" number generator based on the system clock, no matter what language you are using.
"Be bold, and mighty forces will come to your aid."
Malcolm
Posts: 32040
Joined: Fri May 21, 2004 1:04 pm
Location: Minneapolis

Post 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
Diogenes of Sinope: "It is not that I am mad, it is only that my head is different from yours."
Arnold Judas Rimmer, BSC, SSC: "Better dead than smeg."
TheCatt
Site Admin
Posts: 58739
Joined: Thu May 20, 2004 11:15 pm
Location: Cary, NC

Post by TheCatt »

Show me your code that wasn't working. Then, I'll tell you why it wasn't working.
It's not me, it's someone else.
Malcolm
Posts: 32040
Joined: Fri May 21, 2004 1:04 pm
Location: Minneapolis

Post 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
Diogenes of Sinope: "It is not that I am mad, it is only that my head is different from yours."
Arnold Judas Rimmer, BSC, SSC: "Better dead than smeg."
GORDON
Site Admin
Posts: 56735
Joined: Sun Jun 06, 2004 10:43 pm
Location: DTManistan
Contact:

Post 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.
"Be bold, and mighty forces will come to your aid."
Malcolm
Posts: 32040
Joined: Fri May 21, 2004 1:04 pm
Location: Minneapolis

Post 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.
Diogenes of Sinope: "It is not that I am mad, it is only that my head is different from yours."
Arnold Judas Rimmer, BSC, SSC: "Better dead than smeg."
TheCatt
Site Admin
Posts: 58739
Joined: Thu May 20, 2004 11:15 pm
Location: Cary, NC

Post 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
It's not me, it's someone else.
Malcolm
Posts: 32040
Joined: Fri May 21, 2004 1:04 pm
Location: Minneapolis

Post 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.
Diogenes of Sinope: "It is not that I am mad, it is only that my head is different from yours."
Arnold Judas Rimmer, BSC, SSC: "Better dead than smeg."
TheCatt
Site Admin
Posts: 58739
Joined: Thu May 20, 2004 11:15 pm
Location: Cary, NC

Post 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.
It's not me, it's someone else.
Malcolm
Posts: 32040
Joined: Fri May 21, 2004 1:04 pm
Location: Minneapolis

Post 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.
Diogenes of Sinope: "It is not that I am mad, it is only that my head is different from yours."
Arnold Judas Rimmer, BSC, SSC: "Better dead than smeg."
TheCatt
Site Admin
Posts: 58739
Joined: Thu May 20, 2004 11:15 pm
Location: Cary, NC

Post 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"
It's not me, it's someone else.
Malcolm
Posts: 32040
Joined: Fri May 21, 2004 1:04 pm
Location: Minneapolis

Post 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.
Diogenes of Sinope: "It is not that I am mad, it is only that my head is different from yours."
Arnold Judas Rimmer, BSC, SSC: "Better dead than smeg."
TheCatt
Site Admin
Posts: 58739
Joined: Thu May 20, 2004 11:15 pm
Location: Cary, NC

Post 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.
It's not me, it's someone else.
GORDON
Site Admin
Posts: 56735
Joined: Sun Jun 06, 2004 10:43 pm
Location: DTManistan
Contact:

Post by GORDON »

Zing!
"Be bold, and mighty forces will come to your aid."
TheCatt
Site Admin
Posts: 58739
Joined: Thu May 20, 2004 11:15 pm
Location: Cary, NC

Post by TheCatt »

It's not me, it's someone else.
Malcolm
Posts: 32040
Joined: Fri May 21, 2004 1:04 pm
Location: Minneapolis

Post by Malcolm »

TheCatt wrote:Here you go.
Damn, I actually want one now.
Diogenes of Sinope: "It is not that I am mad, it is only that my head is different from yours."
Arnold Judas Rimmer, BSC, SSC: "Better dead than smeg."
Malcolm
Posts: 32040
Joined: Fri May 21, 2004 1:04 pm
Location: Minneapolis

Post 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.
Diogenes of Sinope: "It is not that I am mad, it is only that my head is different from yours."
Arnold Judas Rimmer, BSC, SSC: "Better dead than smeg."
Vince
Posts: 8625
Joined: Thu May 20, 2004 10:00 pm
Location: In bed with your mom

Post 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.
"... and then I was forced to walk the Trail of Tears." - Elizabeth Warren
Post Reply