bugfix: random generator was initialized differently at load vs reload

This commit is contained in:
fros4943 2009-06-24 07:56:15 +00:00
parent 92f8ac6a2b
commit 5d20b01f04
2 changed files with 26 additions and 22 deletions

View File

@ -24,7 +24,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: GUI.java,v 1.137 2009/06/15 18:13:45 fros4943 Exp $
* $Id: GUI.java,v 1.138 2009/06/24 07:56:15 fros4943 Exp $
*/
package se.sics.cooja;
@ -2222,9 +2222,8 @@ public class GUI extends Observable {
try {
shouldRetry = false;
myGUI.doRemoveSimulation(false);
Simulation newSim = loadSimulationConfig(root, true);
Simulation newSim = loadSimulationConfig(root, true, new Long(randomSeed));
myGUI.setSimulation(newSim, false);
myGUI.getSimulation().setRandomSeed(randomSeed);
if (autoStart) {
newSim.startSimulation();
@ -3064,7 +3063,7 @@ public class GUI extends Observable {
Document doc = builder.build(file);
Element root = doc.getRootElement();
return loadSimulationConfig(root, quick);
return loadSimulationConfig(root, quick, null);
} catch (JDOMException e) {
logger.fatal("Config not wellformed: " + e.getMessage());
return null;
@ -3081,7 +3080,7 @@ public class GUI extends Observable {
Document doc = builder.build(stringReader);
Element root = doc.getRootElement();
return loadSimulationConfig(root, quick);
return loadSimulationConfig(root, quick, null);
} catch (JDOMException e) {
throw (SimulationCreationException) new SimulationCreationException(
"Configuration file not wellformed: " + e.getMessage()).initCause(e);
@ -3091,7 +3090,7 @@ public class GUI extends Observable {
}
}
private Simulation loadSimulationConfig(Element root, boolean quick)
private Simulation loadSimulationConfig(Element root, boolean quick, Long manualRandomSeed)
throws SimulationCreationException {
Simulation newSim = null;
@ -3153,7 +3152,7 @@ public class GUI extends Observable {
Collection<Element> config = ((Element) element).getChildren();
newSim = new Simulation(this);
System.gc();
boolean createdOK = newSim.setConfigXML(config, !quick);
boolean createdOK = newSim.setConfigXML(config, !quick, manualRandomSeed);
if (!createdOK) {
logger.info("Simulation not loaded");
return null;

View File

@ -24,7 +24,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: Simulation.java,v 1.47 2009/05/26 14:15:41 fros4943 Exp $
* $Id: Simulation.java,v 1.48 2009/06/24 07:56:15 fros4943 Exp $
*/
package se.sics.cooja;
@ -278,7 +278,7 @@ public class Simulation extends Observable implements Runnable {
}
/**
* Creates a new simulation with a delay time of 100 ms.
* Creates a new simulation
*/
public Simulation(GUI gui) {
myGUI = gui;
@ -348,6 +348,13 @@ public class Simulation extends Observable implements Runnable {
return randomSeed;
}
/**
* @return Random seed (converted to a string)
*/
public String getRandomSeedString() {
return Long.toString(randomSeed);
}
/**
* @param randomSeed Random seed
*/
@ -462,20 +469,16 @@ public class Simulation extends Observable implements Runnable {
}
/**
* Sets the current simulation config depending on the given XML elements.
*
* @see #getConfigXML()
* @param configXML
* Config XML elements
* @param visAvailable
* True if simulation is allowed to show visualizers while loading
* the given config
* @return True if simulation config set successfully
* @throws Exception
* If configuration could not be loaded
* Sets the current simulation config depending on the given configuration.
*
* @param configXML Simulation configuration
* @param visAvailable True if simulation is allowed to show visualizers
* @param manualRandomSeed Simulation random seed. May be null, in which case the configuration is used
* @return True if simulation was configured successfully
* @throws Exception If configuration could not be loaded
*/
public boolean setConfigXML(Collection<Element> configXML,
boolean visAvailable) throws Exception {
boolean visAvailable, Long manualRandomSeed) throws Exception {
// Parse elements
for (Element element : configXML) {
@ -492,7 +495,9 @@ public class Simulation extends Observable implements Runnable {
// Random seed
if (element.getName().equals("randomseed")) {
if (element.getText().equals("generated")) {
if (manualRandomSeed != null) {
setRandomSeed(manualRandomSeed);
} else if (element.getText().equals("generated")) {
randomSeedGenerated = true;
setRandomSeed(new Random().nextLong());
} else {