Random # not so random....

Started by
7 comments, last by CyberSlag5k 20 years ago
I''m doing a game in Java that''s kinda like a multiplayer breakout. It''s done and now I''m just adding some neat little features like powerups for breaking various bricks. I would like the bricks themselves to decide whether or not they contain these power ups so I have imported java.util.* and have created a random number generator (creatively called "random") within the class. Kinda looks like this:

//block class

class Block extends Object
{
    private int type;
    private Random random = new Random();
    public Block()
    {
        width = 80;
        height = 25;
        posX = 400;
        posY = 300;
        type = random.nextInt(2);
        if(type == 1)
            immortal = true;
    }
Obviously there''s more to the class than that, but that''s really just the relavent part. Now, what this says to me is that every time a ball is created, it''s constructor sets its width, height, position, and rolls up a random number (in this case 0 or 1, small just for testing purposes) and if the number is 1 then it makes the block immortal (can''t be destroyed, in other words). The blocks are all part of an array that is initialized as such:

        for(int i = 0; i < 10; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                blockArray[arrayCount] = new Block();
                blockArray[arrayCount].setPos(i*80, j*25);
                arrayCount++;
            }
        }
So.....each element should be created individually and since random is seeded based on time, each block should have a different value for type, right? Wrong. Either all of the blocks are normal or all are immortal. Is it the way the array is set up? Or perhaps java constructors work differently than C++ (my native language)? Or some other weird thing I don''t know about?Any and all help is appreciated. Thanks, Mike When you find yourself in the company of a halfling and an ill-tempered Dragon, remember, you do not have to outrun the Dragon...
Without order nothing can exist - without chaos nothing can evolve.
Advertisement
Er... it looks like ''type'' is in your Block object data but ''immortal'' isn''t... if you want a characteristic to vary per-block, then it needs to be... an attribute of the block, oddly enough.

Oh, you probably also only want to create one Random object. Making multiple Randoms (which you do; one per block) probably amounts to the same thing as calling srand() multiple times in C - bad news.
Moved the random stuff to the main program and created a mutator for the type. All is well.

Thank you.

Mike

When you find yourself in the company of a halfling and an ill-tempered Dragon, remember, you do not have to outrun the Dragon...
Without order nothing can exist - without chaos nothing can evolve.
you know the random function isn''t random at all it''s just a sequence of numbers.
It''s random enough, especially if you seed it using the current time.

When you find yourself in the company of a halfling and an ill-tempered Dragon, remember, you do not have to outrun the Dragon...
Without order nothing can exist - without chaos nothing can evolve.
quote:Original post by CyberSlag5k
It''s random enough, especially if you seed it using the current time.


the reason it wasn''t working was exactly that... since the code runs so fast, the time didn''t have time to change, so you where seeding each random number with the same seed -> you get the same number each time.
And since seeding is sometimes a bit slow with a lot of generators... seed at program init and forget it henceforth.
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
Seed it by program start up time, not by computer time.

Diablo I had their drops determined by computer time, and players would just freeze their clock and get the same drop from every monster.
-Unsuspected
Ok, that makes sense.

Thanks!

Mike

When you find yourself in the company of a halfling and an ill-tempered Dragon, remember, you do not have to outrun the Dragon...
Without order nothing can exist - without chaos nothing can evolve.

This topic is closed to new replies.

Advertisement