• Post Reply Bookmark Topic Watch Topic
  • New Topic

Randomizing names from a list  RSS feed

 
Dave Nice
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgive the beginner question.

I'm trying to use the DataFactory class to pull in a list of names, as I need to populate my app in development with randomized test data for testing.

DataFactory

So, I can get a random name like so:-



But - it always prints 'Lindsey'. I need a way of completely randomizing but I can't see the class has a method to let me do this? I can use the randomize method but this takes an int argument which will always bring back the same index value (same name).

I wonder if I am over complicating this. I can use a simple String array of names, but I don't want, like 10,000 names in my array and I want a good way of generating good, randomized data. Perhaps a .csv file of names could be read in? Here is a second method I wrote but I don't have the skills to know how to read the array values from my large .csv file:



Any help much appreciated.

 
Paul Clapham
Sheriff
Posts: 22831
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused. I don't see any lists in your code; as far as I can tell your DataFactory returns a single name and not a list of names. (I say "as far as I can tell" because the DataFactory link you provided doesn't point to any information about DataFactory. It points to some JDK source code for Random.) And you create a Random object but don't use it anywhere.

And you mentioned you have a .csv file, but I can't see how (or whether) your code is using it.

You also mentioned a "randomize" method -- is that a method of DataFactory?
 
Dave Nice
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,

thank you - I posted in haste and evidently made some mistakes.

I did indeed link to Random class and not DataFactory, which is here:
https://github.com/swung/datafactory/blob/master/src/org/datafactory/DataFactory.java

I think my problem is that I'm primarily a selenium user, who is using Java to empower my use of selenium. So this may not be the best place for questions.

However, my question remains as to how best to generate random names from a large pool, without having to store these names in an array within the class. I want to abstract the data away from the code to simplify things.

The second example I gave works fine, I'm just struggling to find an answer to how I can read names from a .csv or .xls instead of holding them in the array itself. If I want, say, 1000 names to choose from, this is not practical in the current method.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try methods like shuffle, which is (I think) in the Collections class. Or you can try random numbers up to the size of your list of names and use those numbers as an index. For both those techniques you would have to put the names into a List. Selection from an ArrayList runs in constant time.
Note the standard Java installation has several random classes; you might find something other than java.util.Random fits your purposes better. I do not know.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Storing names in a List or an array is probably not expensive on memory; it probably averages out as 8 bytes (on a 64 bit box) per name reference in an array, plus a few for the array object.
 
Paul Clapham
Sheriff
Posts: 22831
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still confused. You posted a link to some source code for a class you claim to be using in your code. (By the way it would be much more useful to have a link to the documentation for the class.) However you're calling a "getFirstName" method, and that code doesn't have any such method.

Wait a minute... you're using "org.fluttercode.datafactory.impl.DataFactory" and the code you linked to is "org.datafactory.DataFactory". That explains the discrepancy.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!