Reputation: 115
im doing a performance review of graphdatabases in context of process modelling. Therefore I set up a simple testcase which tracks the performance of simple inserts into the embedded N4J database. This is an excerpt of my testcase.
@Test
public void insertQuants(){
int size =750;
/**
* create 10 different languages and store them in the ArrayList
*/
ArrayList<Language> lngs = new ArrayList<Language>();
for(int i=0; i<10; i++){
String title = "Testsprache " + String.valueOf(i);
String description = "Beschreibung " +String.valueOf(i);
lngs.add(modelservice.createLanguage(title,description));
}
/**
* Create different models and assign them to different
* language objects
*/
for(int i=0; i<size; i++){
String title = "Testmodel " + String.valueOf(i);
//get random between 0 and 9
int fin = (int) Math.round(Math.random()*9);
//fetch random Language object out of ArrayList
Language l = lngs.get(fin);
Model m = modelservice.createModel(l, title);
l.addModel(m);
template.save(l);
}
the methods createModel in modelservice return the specific created object ( model or language ). The issue i'm currently facing is that the random assigning of languages to models is not working. The Script assigns each model to one language object. In my mind it rather should randomly assign languages to model.
Upvotes: 2
Views: 11185
Reputation: 9008
This isn't your problem, but it's a problem: Math.random returns a double between 0 and 1. Multiplying that by 9 and rounding doesn't give you a fair distribution (zero and nine will appear only half as often as you'd want). Change:
int fin = (int) Math.round(Math.random()*9);
to something more like
Random randomGenerator = new Random();
...
int fin = randomGenerator.nextInt(10);
Upvotes: 1
Reputation: 14699
The problem is not with the random assignment:
int fin = (int) Math.round(Math.random()*9);
Math.random()
returns a double
in range: [0,1)
, multiplying by 9 gives: [0,9)
, and rounding gives [0,9]
. As CPerkins pointed out, using Random
may look simpler and is probably better (since the probability of generating numbers 0 through 9 is not uniform with your method), but your random number generation range is correct. The issue is not in the code that you have shown us. Perhaps it's in addModel()
or save()
.
Upvotes: 2