• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Random # not so random....

## Recommended Posts

CyberSlag5k    514
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...

##### Share on other sites
Zahlman    1682
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.

##### Share on other sites
CyberSlag5k    514
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...

##### Share on other sites
you know the random function isn''t random at all it''s just a sequence of numbers.

##### Share on other sites
CyberSlag5k    514
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...

##### Share on other sites
SpaceDude    208
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.

##### Share on other sites
Wyrframe    2426
And since seeding is sometimes a bit slow with a lot of generators... seed at program init and forget it henceforth.

##### Share on other sites
Unsuspected    122
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.

##### Share on other sites
CyberSlag5k    514
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...