XNA Creators Club Online
Page 1 of 2 (34 items) 1 2 Next >
Sort Posts: Previous Next

random generator broken?

Last post 10/21/2009 8:54 PM by jwatte. 33 replies.
  • 9/23/2009 11:44 PM

    random generator broken?

    are there any known issues with the Random class on the 360?  i create 1 instance of Random for my entire game, and keep calling NextDouble() and NextInt()  as needed.  on pc it's fine, but after a  while on the 360 it starts spitting out the same number all tthe time.  has anybody else seen this?
    Coming soon! Zulu Hour.
  • 9/23/2009 11:47 PM In reply to

    Re: random generator broken?

    Have you confirmed this behavior consistently? It could have just randomly repeated the same number a few times.
    Previously known as "Rainault".
    Twitter - me, Jade Vault Games
    Announcing ASCII Quest, a Roguelike under development for Xbox LIVE Indie Games
  • 9/23/2009 11:50 PM In reply to

    Re: random generator broken?

    Do you have a code snippet? If you aren't calling "new Random();" all the time you shouldn't get that problem.

    Can you paste the following:
    -The code you use to instantiate the Random object (Your "new Random()" call)
    -Some examples of your usage of .Next() and NextDouble()
  • 9/24/2009 12:11 AM In reply to

    Re: random generator broken?

    Yes.  And once it's stuck, it's stuck for good until i restart the game.

    it's happens semi reliably.  i know this means nothing to you guys,, but it only seems to happen in 4-player arcade mode, so i doubt i can make a simple program that demonstrates the problem.  i can try tho!
    Coming soon! Zulu Hour.
  • 9/24/2009 12:14 AM In reply to

    Re: random generator broken?

    Rook 0x51:
    Do you have a code snippet? If you aren't calling "new Random();" all the time you shouldn't get that problem.

    Can you paste the following:
    -The code you use to instantiate the Random object (Your "new Random()" call)
    -Some examples of your usage of .Next() and NextDouble()


    Sure...Pretty straightforward, but here it is:

        public class Utility  
        {  
     
            private static Random rand = new Random();  
            public static int getRandomInt(int min, int max)  
            {  
                return rand.Next(min, max);  
            }  
            public static float getRandomFloat(float min, float max)  
            {  
                return (float)rand.NextDouble() * (max - min) + min;  
            }  
            public static float getRandomFloat()  
            {  
                return (float)rand.NextDouble();  
            }  
    ...  
    Coming soon! Zulu Hour.
  • 9/24/2009 12:14 AM In reply to

    Re: random generator broken?

    Just copy/paste the method that seems to have .Next and .NextDouble repeating the same value. Maybe we can see something there.
  • 9/24/2009 12:16 AM In reply to

    Re: random generator broken?

    Can I see the code that you call Utility.getRandomFloat from in your 4-player arcade?
  • 9/24/2009 12:25 AM In reply to

    Re: random generator broken?

    also in Utility {}:

            public static Vector2 getRandomVec2()  
            {  
                return new Vector2(getRandomFloat(), getRandomFloat());  
            }  
            public static Vector2 getRandomVec2(Vector2 min, Vector2 max)  
            {  
                return new Vector2(getRandomFloat(min.X, max.X), getRandomFloat(min.Y, max.Y));  
            }  
            public static Vector3 getRandomVec3()  
            {  
                return new Vector3(getRandomFloat(), getRandomFloat(), getRandomFloat());  
            }  
            public static Vector3 getRandomVec3(Vector3 min, Vector3 max)  
            {  
                return new Vector3(getRandomFloat(min.X, max.X), getRandomFloat(min.Y, max.Y),  
                    getRandomFloat(min.Z, max.Z));  
            } 


    I use  getRandomVec3 all over the place; I doubt cluttering this with those calls would be very helpful.  I have verified min and max are good.
    Coming soon! Zulu Hour.
  • 9/24/2009 12:47 AM In reply to

    Re: random generator broken?

    Hi,

    Please implement this class instead of Random and let me know if you still have the same issue (sorry for the usage of some Italian in the names):

        public class SecureRandom  
        {  
            private int[] primi = new int[] {5,7,11,23,47,59,83,107,167,179,227,263,347,359,  
                                                   383,467,479,503,563,587,719,839,863,887,983,1019,  
                                                   1187,1283,1307,1319,1367,1439,1487,1523,1619,1823,  
                                                   1907,2027,2039,2063,2099,2207,2447,2459,2579,2819,  
                                                   2879,2903,30593,19333,16141,15683,81463,28229,31907,  
                                                   19609,35617,82073,44293,43331,34061,89689,162143,  
                                                   134513,173359,31397,404597,212701,188029,542603,  
                                                   265621,461717,155921,544279,404851,927869,  
                                                   1100977,360653,604073};  
     
            private Random generatoreVero, generatoreIniziale;  
     
            public SecureRandom()  
            {  
                generatoreIniziale = new Random();  
                int[] chiave = new int[5];  
                for (int i = 0; i < 5; i++)  
                    chiave[i] = generatoreIniziale.Next(primi.Length - 1);  
                int seed = primi[chiave[0]] + primi[chiave[1]] + primi[chiave[2]] + primi[chiave[3]] + primi[chiave[4]];  
                generatoreVero = new Random(seed);  
                generatoreIniziale = null;  
            }  
     
            public int Next()  
            {  
                return generatoreVero.Next();  
            }  
     
            public int Next(int Max)  
            {  
                return generatoreVero.Next(Max);  
            }  
     
            public int Next(int Min, int Max)  
            {  
                return generatoreVero.Next(Min, Max);  
            }  
     
            public void NextBytes(byte[] buffer)  
            {  
                generatoreVero.NextBytes(buffer);  
            }  
     
            public double NextDouble()  
            {  
                return generatoreVero.NextDouble();  
            }  
        }  
     

    Cheers,
    Pino

  • 9/24/2009 1:04 AM In reply to

    Re: random generator broken?

    I always used System.Cryptography.RNGCryptoServiceProvider instead of the Random class, simply because it can create different numbers during the same update. I've always liked that.
    Freefall Game Engine - In Development
  • 9/24/2009 1:08 AM In reply to

    Re: random generator broken?

    Hmmm... everything looks fine. Does it return the same number every time you use it? Are there any cases that it does generate a new number? If you are saying that it works through your game except in a particular section/mode, I would say verify that the places you call Utility methods from are correct. I know you said min and max are good, but triple-check anyway. Try changing min and max to different numbers and checking the result.

    Other than that I don't see what else could be wrong.
  • 9/24/2009 1:38 AM In reply to

    Re: random generator broken?

    PinoEire:
    Please implement this class instead of Random and let me know if you still have the same issue (sorry for the usage of some Italian in the names):


    thanks for the suggestion  pino.  unfortunately, it still has the same issues.
    Coming soon! Zulu Hour.
  • 9/24/2009 1:39 AM In reply to

    Re: random generator broken?

    ShadowyCore:
    I always used System.Cryptography.RNGCryptoServiceProvider instead of the Random class, simply because it can create different numbers during the same update. I've always liked that.


    hmm, i'll definitely check it out.  thanks!
    Coming soon! Zulu Hour.
  • 9/24/2009 1:42 AM In reply to

    Re: random generator broken?

    Crying Monkey Studio:
    PinoEire:
    Please implement this class instead of Random and let me know if you still have the same issue (sorry for the usage of some Italian in the names):


    thanks for the suggestion  pino.  unfortunately, it still has the same issues.


    That should be a good indication that it isn't the Random object returning bogus values. Quadruple-check the areas you call you it to make sure you don't have casting errors or other issues.
  • 9/24/2009 1:45 AM In reply to

    Re: random generator broken?

    Rook 0x51:
    Hmmm... everything looks fine. Does it return the same number every time you use it? Are there any cases that it does generate a new number? If you are saying that it works through your game except in a particular section/mode, I would say verify that the places you call Utility methods from are correct. I know you said min and max are good, but triple-check anyway. Try changing min and max to different numbers and checking the result.

    Other than that I don't see what else could be wrong.


    It works for a few minutes, then just keeps giving the same number.  i've checked min and max (via breakpoints in vs) and have good values.  also, getNextInt() starts giving the same values as well.  I will look at it more tomorrow, but it's sure looking like a bug in xna/clr since it works on the pc.
    Coming soon! Zulu Hour.
  • 9/24/2009 1:47 AM In reply to

    Re: random generator broken?

    Rook 0x51:
    Crying Monkey Studio:
    PinoEire:
    Please implement this class instead of Random and let me know if you still have the same issue (sorry for the usage of some Italian in the names):


    thanks for the suggestion  pino.  unfortunately, it still has the same issues.


    That should be a good indication that it isn't the Random object returning bogus values. Quadruple-check the areas you call you it to make sure you don't have casting errors or other issues.


    How do you make that conclusion?  it still uses the Random class.
    Coming soon! Zulu Hour.
  • 9/24/2009 2:01 AM In reply to

    Re: random generator broken?

    Crying Monkey Studio:
    Rook 0x51:
    Hmmm... everything looks fine. Does it return the same number every time you use it? Are there any cases that it does generate a new number? If you are saying that it works through your game except in a particular section/mode, I would say verify that the places you call Utility methods from are correct. I know you said min and max are good, but triple-check anyway. Try changing min and max to different numbers and checking the result.

    Other than that I don't see what else could be wrong.


    It works for a few minutes, then just keeps giving the same number.  i've checked min and max (via breakpoints in vs) and have good values.  also, getNextInt() starts giving the same values as well.  I will look at it more tomorrow, but it's sure looking like a bug in xna/clr since it works on the pc.


    If it is a bug, it should be reproducable, then.  If you aren't using a specific seed, I'd suggest trying it with one.  If it still causes the issue, then get some logging information about how many calls it takes before it starts spitting out the same number.
  • 9/24/2009 7:28 AM In reply to

    Re: random generator broken?

    Crying Monkey Studio:
    PinoEire:
    Please implement this class instead of Random and let me know if you still have the same issue (sorry for the usage of some Italian in the names):


    thanks for the suggestion  pino.  unfortunately, it still has the same issues.
    Hi,

    my calss reduces the activity of the Random class to a safe, secure and well known field of operation. If even implementing that you got a long sequence of identical numbers I can olny gather that you (1) have a problem only where you correct the random result using min and max (or other correctors), with those values corrupted in some way or (2) your logic recovers somehow a previous value or (3) for some reason you're not actually calling anymore the random generation method.

    Cheers,
    Pino
  • 9/24/2009 2:44 PM In reply to

    Re: random generator broken?

    Rather than looking at lots of code and rewriting it how about you just take the standard xbox game template and add 2 lines of code


    Random r = new Random();    in the declarations

    Debug.WriteLine(r.NextDouble());   in Update()

    Then run it and watch the debug output... it should be pretty obvious when/if it starts repeating*. If it does... you have a repro to give to the XNA team. If it doesn't then slowly add back in the bits and pieces that you have, static classes, more complex functions etc..

    It would seem quite unlikley (but not 0%) that the Random function would have a bug, the algorithms used are quite standard and I doubt anything had to change between the desktop and the CF CLRs.. its a bit of simple math.




    *Note I'm running this on my 360 right now.... no repeats at all that I can see after a good 5+ minutes of running.

     

     

     

     

    Play Kissy Poo - a game for 4 year olds on Xbox and windows
    The ZBuffer
    News and information for XNA
      Follow The Zman on twitter, Email me
        Please read the forum FAQs - Bug/Feature reporting
          Don't forget to mark good answers and good playtest feedback when you see it!!!
  • 9/24/2009 5:52 PM In reply to

    Re: random generator broken?

    Just for kicks, I created a xbox game and put random number generation and duplicate counting in a separate thread with display in the main thread.  This runs at about 10,000,000 random numbers per second.

    Using myRandom.Next(), it went though 2,300,000,000 random numbers without a duplicate.  Switching to myRandom.Next(100) for a sanity check produced about 1 duplicate per 10,000 random numbers in 300,000,000 iterations, as expected.

    This was with a fixed seed of 1520193476.

    The Xbox Random class looks ok at this point, although it is hard to generate good pseudo-random numbers and easy to mess them up.

    The problem is probably downstream from the random number generator (especially since swapping in Pino's generator didn't change the behavior).
    Three colors, endless fun ... Primary Attack
  • 9/24/2009 6:39 PM In reply to

    Re: random generator broken?

    Try this random number generator instead of Random. It's a very weak generator, but if your program never repeats numbers with this generator, but does repeat with System.Random, then it's either System.Random or some random heap corruption. If it still hangs, then it's likely something in your code. If it works with this generator, then you can use it as a work-around, or write your own if you want higher quality random numbers.

      class TestRandom { 
        public TestRandom() { state = (ulong)DateTime.Now.Ticks; } 
        ulong state; 
        public int Next() { return Next(0, int.MaxValue); } 
        public int Next(int max) { return Next(0, max); } 
        public int Next(int min, int max) { 
          lock(this) { 
            state = state * 0x7f13dd + 0x998853; 
            return (int)(((state >> 8) & 0xffffffffUL) * (ulong)(max - min) / 0x100000000UL); 
          } 
        } 
        public void NextBytes(byte[] data) { 
          for (int i = 0; i < data.Length; ++i) { 
            data[i] = (byte)Next(256); 
          } 
        } 
        public double NextDouble() { 
          return (double)Next(1000000000) / 1e9; 
        } 
      } 
     

    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 9/26/2009 4:58 PM In reply to

    Re: random generator broken?

    jwatte:
    Try this random number generator instead of Random. It's a very weak generator, but if your program never repeats numbers with this generator, but does repeat with System.Random, then it's either System.Random or some random heap corruption. If it still hangs, then it's likely something in your code. If it works with this generator, then you can use it as a work-around, or write your own if you want higher quality random numbers.


    did you mistype something?  i'm getting nothing but 0...
    Coming soon! Zulu Hour.
  • 9/26/2009 5:30 PM In reply to

    Re: random generator broken?

    I copy and pasted his class exactly and both Next() and NextDouble() work for me.
  • 9/26/2009 6:03 PM In reply to

    Re: random generator broken?

    wow, this is weird.  i'm doing  rand.next(5, 15) and i'm getting 1.

    something smells funky...maybe it's me, not sure yet.
    Coming soon! Zulu Hour.
  • 9/26/2009 6:43 PM In reply to

    Re: random generator broken?

    How are you checking the value?  Are you still using your code?  I just tested 5,15 and got an assortment of values as expected.
Page 1 of 2 (34 items) 1 2 Next > Previous Next