Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [General Java] Random.nextInt() having a very obvious pattern?  (Read 471 times)
0 Members and 1 Guest are viewing this topic.
Offline Willchill
« Posted 2014-05-11 09:13:24 »

Hello,

I've been working on a system of randomly generating a level. The code can be found here. http://pastebin.com/4eddVXE8


The issue I'm having at the moment is illustrated in this screenshot. The generation appears to be happening in diagonal lines. How could this be fixed so that the generation is truly random?

Any help is greatly appreciated.
Online trollwarrior1
« Reply #1 - Posted 2014-05-11 09:14:58 »

x + y * width and not x + y
Offline Willchill
« Reply #2 - Posted 2014-05-11 09:18:09 »

x + y * width and not x + y
If you meant that in the index for tiles[] in the getTile method, it throws an ArrayIndexOutOfBoundsException. int tiles[] is declared with a size of width * height.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline LiquidNitrogen
« Reply #3 - Posted 2014-05-11 09:21:25 »

1  
2  
                for(int x = 0; x < width; x++){
                        for(int y = 0; y < height; y++){


this will probably fix the array out of of bounds error when using what trollwarrior said.
Online trollwarrior1
« Reply #4 - Posted 2014-05-11 09:29:10 »

1  
2  
3  
4  
5  
int getTile(int x, int y){
     if(x < 0 || x >= width || y < 0 || y >= height) return 0;

     return tiles[x + y * width];
}


EDIT-
what liquid said should fix it.
Offline Willchill
« Reply #5 - Posted 2014-05-11 09:32:18 »

1  
2  
3  
4  
5  
int getTile(int x, int y){
     if(x < 0 || x >= width || y < 0 || y >= height) return 0;

     return tiles[x + y * width];
}

That worked, thanks! Cheesy
Offline LiquidNitrogen
« Reply #6 - Posted 2014-05-11 09:39:13 »

you might find it easier using a 2d array for your map anyway.. its generally more intuitive to work with.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #7 - Posted 2014-05-11 13:23:36 »

There are generally very few reasons to "flatten" a 2D array into a 1D array. Damn, theCherno has taught a lot of people some bad coding practices... Just stick with 2D arrays.

Online theagentd
« Reply #8 - Posted 2014-05-12 02:28:55 »

There are generally very few reasons to "flatten" a 2D array into a 1D array. Damn, theCherno has taught a lot of people some bad coding practices... Just stick with 2D arrays.
There are lots of good reasons not to use 2D arrays, especially if they are small. You can easily abstract it all away with a point-to-index and a index-to-point function.

Myomyomyo.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #9 - Posted 2014-05-12 02:52:04 »

Fair enough, can you give me some reasons? From what I've read the differences (except for in large arrays) are negligible.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online BurntPizza
« Reply #10 - Posted 2014-05-12 02:57:59 »

Fair enough, can you give me some reasons? From what I've read the differences (except for in large arrays) are negligible.

There was a recent discussion (including benchmarks, oh my) here: http://www.java-gaming.org/topics/is-there-a-logical-reason-to-stuff-2d-array-data-into-one/33042/view.html

Actually small arrays are affected the most, however they also take the least time to process, so yes, it's usually only worthwhile to optimize large arrays.
It's mostly a matter of cache-aware programming. Make of it what you will.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #11 - Posted 2014-05-12 03:06:54 »

Thanks! Interesting stuff.

Offline Roquen
« Reply #12 - Posted 2014-05-12 05:23:04 »

That thread was microbenchmarks and doesn't consider generalized real access patterns in time.  nD arrays are significantly slower in practice.  The math tells you so.  It also doesn't consider the exponential increased burden on the GC which in turn effects overall (not localized) performance.  If overall performance isn't a concern it doesn't matter so much.

(EDIT: Since this is a newbie post, something no mentioned in the linked thread is that in the microbenchmark the memory layouts of the 1D and 2D are virtually identical since the 2D array is allocated before any GCs occur).
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (17 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (67 views)
2014-08-11 02:49:23

BurntPizza (45 views)
2014-08-09 21:09:32

BurntPizza (36 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!