Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 10-16-09   #1 (permalink)
New to Overclock.net
 
intel nvidia

Join Date: Oct 2007
Location: Queens, NY
Posts: 2,994
Blog Entries: 1

Rep: 216 ninjinsamax3 is acknowledged by manyninjinsamax3 is acknowledged by manyninjinsamax3 is acknowledged by many
Unique Rep: 174
Hardware Reviews: 11
Trader Rating: 8
Default JAVA: Randoming amount of objects added to World

I need help on randomizing the number of objects added to the world.

public void addedToWorld(World world)
{
this.world = (AlienWorld)world;
setImage(new GreenfootImage(1, 1));
aliens = new HashSet<Alien>();
for(int i = getX() ; i<=getX()+800 ; i+=50) {
for(int j = getY() ; j<getY()+(ROWS*(Greenfoot.getRandomNumber(50)+1)) ; j+=50) {
Alien alien = new Alien();
world.addObject(alien, i, j);
aliens.add(alien);
}
}
}

Some things to know is that ROWS = 3, so basically there will be 3 rows.
The first row according to this code, adds a whole row of aliens which means it is not randomizing at all.
The second and third row does do what it is doing, randomizing the amount in the row and also the position.

Example: O = object X = space/empty slot
OOOOOOOOOO
OOXOXXOOOXO
XOOOOXOXXXO

When I reset the scenario:
OOOOOOOOOO
XOXOXXOOXOO
OOOXOXXOOXO

When I reset the scenario only row 2 and 3 are randomizing, but the first row is not, and I have no idea how to fix it, anyone know?

Thanks
__________________
I am currently very busy with moving, so if you have helped me and not get what you deserved or any replies from me in PM or on Threads, I apologize. When I have time, I will definitely reply and give you what you guys deserve. Thank you for understanding, since I also lost my summer with this sudden move. I promise that I will get back to all of you guys. OCN honor!
今学校のため、忙しくがんばってる運命の人を探してる中だっ
folding/フォールディング:what is folding?my foldingOCN's 24 Hour Fold-A-Thon
javascript/ジャバスクリプト:profit calculator
for sale/フォー・セール:coming soon
project/プロジェクト:coming soon

System: hikari. (ASUS N50Vn-X1B)
CPU
Intel Mobile Core 2 Duo P8600 2.4Ghz
Motherboard
ASUS N50Vn-X1B
Memory
4GB DDR2
Graphics Card
ASUS NVIDIA GeForce 9650M GT
Hard Drive
250GB SATA 5400rpm
Sound Card
Integrated Sound Card
Power Supply
6-Cell Lithium Ion Battery & 90-watt AC adapter
Case
ASUS N50Vn-X1B
OS
Windows Vista Ultimate 32bit
Monitor
15.4" WXGA/LED
ninjinsamax3 is offline   Reply With Quote
Old 10-16-09   #2 (permalink)
New to Overclock.net
 
adramalech707's Avatar
 
intel nvidia

Join Date: Feb 2008
Posts: 219

Rep: 5 adramalech707 Unknown
Unique Rep: 5
Folding Team Rank: 708
Trader Rating: 0
Default

okay well i don't know if this will work but maybe what you need to do is think about the random number generator....see if the random number generator isn't setup correct or what not you will get repeats......

another tip is maybe name objects different than the objects name.....so it has better readability...

http://java.sun.com/j2se/1.4.2/docs/...il/Random.html

this is the java doc for random...which is the superclass for anything about random number generators and what not....

what you got to worry about is this part....which i quote directly from the documentation of random class

Quote:
If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random. Java implementations must use all the algorithms shown here for the class Random, for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods.
so pretty much gotta worry about the generator mutating and showing bad cases of repetition
__________________
x2 4600+ @2.6ghz

x2 6400+ @ 3.5ghz

Quote:
When you say "I wrote a program that crashed Windows", people just stare at you blankly and say "Hey, I got those with the system, *for free*"
-Linus Torvalds
Quote:
"How should I know if it works? That's what beta testers are for. I only coded it."
-Linus Torvalds
Quote:
Be warned that typing \fBkillall \fIname\fP may not have the desired effect on non-Linux systems, especially when done by a privileged user.
-killall man page

System: Intel build
CPU
i7 860 @ 2.8ghz
Motherboard
gigabyte p55-ud6
Memory
dominator gt CMG4GX3M2A1600C7
Graphics Card
bfg 9600gt oc2
Hard Drive
wd caviar 500gb
Power Supply
750watt toughpower
Case
antec 900
CPU cooling
coolermaster hyper tx3
GPU cooling
stock
OS
windows 7/ gentoo 64bit
Monitor
samsung 2243swx
adramalech707 is offline I fold for Overclock.net   Reply With Quote
Old 10-16-09   #3 (permalink)
4.0 GHz
 
dharmaBum's Avatar
 
intel nvidia

Join Date: Apr 2007
Location: Raleigh, NC
Posts: 747

Rep: 121 dharmaBum is acknowledged by manydharmaBum is acknowledged by many
Unique Rep: 89
Trader Rating: 0
Default

Out of context it isn't too easy to parse your code. But I'll give it a shot.

If I'm understanding you're problem, it's that the first row is consistently being filled with objects. This is expected from how you're "for" loop is set up:

Code:
for(int j = getY() ; j<getY()+(ROWS*(Greenfoot.getRandomNumber(50)+1)) ; j+=50) {...}
Notice: getY()+(ROWS*(rand(50)+1)) > getY(), because the minimum of (ROWS*(rand(50)+1)) is ROWS. And getY < getY + ROWS.

So this loop will always execute one iteration, so you're always going to get a first row full of objects.

At this point I'm not sure if you're trying to completely randomize each spot on a column, or have a "random amount" in each (that is, each column gets either 0,1,2,3 objects filling up from the bottom). Using a for loop in the manner you have suggests the later, but your comment suggests the former (in which case just iterate through the rows and do a "coin flip" to decide if to add an object or not).
__________________
3DMark06: 19091 - 3DMark Vantage: P15264 - SuperPi: 10.968s

Programming Quote of the Day:
Bjarne Stroustrup:
Quote:
There are only two industries that refer to their customers as ‘users’.

System: Europa
CPU
E8500 4.36ghz @ 1.36v
Motherboard
EVGA 780i SLi P05 Bios
Memory
G.SKILL 4GB(2x2GB) @ 924MHz (5-4-4-12-2T)
Graphics Card
2xEVGA 8800GTS (G92) 512MB @800/2000/2110
Hard Drive
Seagate 500gbx2, (fake-)RAID0
Sound Card
Sound Blaster X-Fi XtremeGamer
Power Supply
CORSAIR 1000HX 1000W
Case
Gigabyte GZ-FA2CA-AJB Black Aluminum
CPU cooling
TDX 775 Block, 360 BlackIce rad
GPU cooling
MAZE5x2, TT copper HS
OS
Fedora10-86_64/Vista64
Monitor
22" Samsung SyncMaster 2232BW
dharmaBum is offline   Reply With Quote
Old 10-16-09   #4 (permalink)
New to Overclock.net
 
intel nvidia

Join Date: Oct 2007
Location: Queens, NY
Posts: 2,994
Blog Entries: 1

Rep: 216 ninjinsamax3 is acknowledged by manyninjinsamax3 is acknowledged by manyninjinsamax3 is acknowledged by many
Unique Rep: 174
Hardware Reviews: 11
Trader Rating: 8
Default

Yeah I noticed that problem in the for loop afterward, but I have no idea how to fix this issue. Would you happen to know how I can make it randomized all over the place?

The quesiton that I was asked to do was ambiguous. It only said to make the objects have a random amount. Originally it has liek 3 rows and I believe 17 column, so that makes 51 objects. In the pattern shown above.

I would have put the other classes, but there was too much to put since most were prefilled and i was only assigned to edit some parts.
__________________
I am currently very busy with moving, so if you have helped me and not get what you deserved or any replies from me in PM or on Threads, I apologize. When I have time, I will definitely reply and give you what you guys deserve. Thank you for understanding, since I also lost my summer with this sudden move. I promise that I will get back to all of you guys. OCN honor!
今学校のため、忙しくがんばってる運命の人を探してる中だっ
folding/フォールディング:what is folding?my foldingOCN's 24 Hour Fold-A-Thon
javascript/ジャバスクリプト:profit calculator
for sale/フォー・セール:coming soon
project/プロジェクト:coming soon

System: hikari. (ASUS N50Vn-X1B)
CPU
Intel Mobile Core 2 Duo P8600 2.4Ghz
Motherboard
ASUS N50Vn-X1B
Memory
4GB DDR2
Graphics Card
ASUS NVIDIA GeForce 9650M GT
Hard Drive
250GB SATA 5400rpm
Sound Card
Integrated Sound Card
Power Supply
6-Cell Lithium Ion Battery & 90-watt AC adapter
Case
ASUS N50Vn-X1B
OS
Windows Vista Ultimate 32bit
Monitor
15.4" WXGA/LED

Last edited by ninjinsamax3 : 10-16-09 at 10:37 PM
ninjinsamax3 is offline   Reply With Quote
Old 10-16-09   #5 (permalink)
4.0 GHz
 
dharmaBum's Avatar
 
intel nvidia

Join Date: Apr 2007
Location: Raleigh, NC
Posts: 747

Rep: 121 dharmaBum is acknowledged by manydharmaBum is acknowledged by many
Unique Rep: 89
Trader Rating: 0
Default

Quote:
Originally Posted by ninjinsamax3 View Post
Yeah I noticed that problem in the for loop afterward, but I have no idea how to fix this issue. Would you happen to know how I can make it randomized all over the place?

The quesiton that I was asked to do was ambiguous. It only said to make the objects have a random amount. Originally it has liek 3 rows and I believe 17 column, so that makes 51 objects. In the pattern shown above.

I would have put the other classes, but there was too much to put since most were prefilled and i was only assigned to edit some parts.
If it's supposed to just assign random throughout, then the solution is pretty easy (using simplified code).

Code:
//assume the object array has been initialized to all empty.
void randomize()
{
   int coinflip;
   for(int i=0;i<ROWS;i++)
   {
      for(int j=0;j<COLUMNS;j++)
      {
          //here my_random will return 0 or 1
          coinflip=my_random(1);
          //if 1, add object
          if(coinflip==1)
          {
              add_object(i,j);
          }
       }
}
Is this close to what you're looking to do?
__________________
3DMark06: 19091 - 3DMark Vantage: P15264 - SuperPi: 10.968s

Programming Quote of the Day:
Bjarne Stroustrup:
Quote:
There are only two industries that refer to their customers as ‘users’.

System: Europa
CPU
E8500 4.36ghz @ 1.36v
Motherboard
EVGA 780i SLi P05 Bios
Memory
G.SKILL 4GB(2x2GB) @ 924MHz (5-4-4-12-2T)
Graphics Card
2xEVGA 8800GTS (G92) 512MB @800/2000/2110
Hard Drive
Seagate 500gbx2, (fake-)RAID0
Sound Card
Sound Blaster X-Fi XtremeGamer
Power Supply
CORSAIR 1000HX 1000W
Case
Gigabyte GZ-FA2CA-AJB Black Aluminum
CPU cooling
TDX 775 Block, 360 BlackIce rad
GPU cooling
MAZE5x2, TT copper HS
OS
Fedora10-86_64/Vista64
Monitor
22" Samsung SyncMaster 2232BW
dharmaBum is offline   Reply With Quote
Old 10-17-09   #6 (permalink)
New to Overclock.net
 
intel nvidia

Join Date: Oct 2007
Location: Queens, NY
Posts: 2,994
Blog Entries: 1

Rep: 216 ninjinsamax3 is acknowledged by manyninjinsamax3 is acknowledged by manyninjinsamax3 is acknowledged by many
Unique Rep: 174
Hardware Reviews: 11
Trader Rating: 8
Default

To me it seems great, I didn't think abotu this.

My professor just want's it to have a random amount of objects, instead of all the rows having objects. I guess thsi would work. I will try this on monday when I have access to Uni computers! Thanks a lot.

edit:
Your idea worked great! Thanks again!
__________________
I am currently very busy with moving, so if you have helped me and not get what you deserved or any replies from me in PM or on Threads, I apologize. When I have time, I will definitely reply and give you what you guys deserve. Thank you for understanding, since I also lost my summer with this sudden move. I promise that I will get back to all of you guys. OCN honor!
今学校のため、忙しくがんばってる運命の人を探してる中だっ
folding/フォールディング:what is folding?my foldingOCN's 24 Hour Fold-A-Thon
javascript/ジャバスクリプト:profit calculator
for sale/フォー・セール:coming soon
project/プロジェクト:coming soon

System: hikari. (ASUS N50Vn-X1B)
CPU
Intel Mobile Core 2 Duo P8600 2.4Ghz
Motherboard
ASUS N50Vn-X1B
Memory
4GB DDR2
Graphics Card
ASUS NVIDIA GeForce 9650M GT
Hard Drive
250GB SATA 5400rpm
Sound Card
Integrated Sound Card
Power Supply
6-Cell Lithium Ion Battery & 90-watt AC adapter
Case
ASUS N50Vn-X1B
OS
Windows Vista Ultimate 32bit
Monitor
15.4" WXGA/LED

Last edited by ninjinsamax3 : 4 Weeks Ago at 04:22 PM
ninjinsamax3 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 09:57 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.14792 seconds with 8 queries