Posted by Syed Aslam on February 28, 2008
You might have wondered how predictable machines like computers can generate randomness. In reality, most random numbers used in computer programs are pseudo-random, which means they are generated in a predictable fashion using a mathematical formula. This is fine for many purposes, but it may not be random in the way you expect if you’re used to dice rolls, roulette wheels and lottery draws.
A list of random number is a numerical sequence as defined in Wiki - “A numeric sequence is said to be statistically random when it contains no recognizable patterns or regularities; sequences such as the results of an ideal die roll, or the digits of π (as far as we can tell) exhibit statistical randomness…”
We can write a random number generator to generate a list of random numbers in a given range. “A random number generator (often abbreviated as RNG) is a computational or physical device designed to generate a sequence of numbers or symbols that lack any pattern, i.e. appear random. Computer-based systems for random number generation are widely used, but often fall short of this goal, though they may meet some statistical tests for randomness intended to ensure that they do not have any easily discernible patterns. Methods for generating random results have existed since ancient times, including dice, coin flipping, the shuffling of playing cards, the use of yarrow stalks in the I Ching, and many other techniques.”
Intuitively, an algorithmically random sequence (or random sequence) is an infinite sequence of binary digits that appears random to any algorithm. The definition applies equally well to sequences on any finite set of characters. Random sequences are key objects of study in algorithmic information theory.
I had written a small program in Java to generate a sequence of unique random numbers within 20.
I am creating a
List in which I intend to store unique number between 1 and 20 (inclusive). Firstly, I have to get hold of the object of class
Random In order to store only unique numbers, first I check after generating the random number by
rd.nextInt(20);
whether that number already exists in the list. And, thats it. You have got a list of unique random numbers between 1 and 20.
Blogged with Flock
Tags: random, List, random number generator, rand, sequence
Posted in java | No Comments »
Posted by Syed Aslam on February 28, 2008
Posted in fun, general | No Comments »
Posted by Syed Aslam on February 26, 2008
A Prepared Statement forces a SQL statement to be set to the database immediately where it will be precompiled. This means that for subsequent calls, the SQL statement can just run the Prepared Statements’ SQL statement without having to compile it with every call. A Prepared Statement is most useful when using a SQL statement that accepts parameters that may change with every call. I’ll demonstrate this technique using a MySQL database.
Below is a method that sends a Prepared Statement to the database with two parameters: “userId” and “password”. Notice that it calls the getConnection () method from a class instance called dataUtil. We’ll talk about this class in a second for completeness.
public ResultSet viewItem (int journalId, int userId) throws SQLException {
ResultSet rs = null;
String sql = “SELECT journal_id, date_added, journal_text FROM journal” +
“WHERE journal_id = ? AND user_id = ?”;
Connection conn = dataUtil.getConnection();
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setInt(1, journalId);
preparedStatement.setInt(2, userId);
rs = preparedStatement.executeQuery();
return rs;
}
As I mentioned above, I wanted to show you how to use the getConnection method, because after all, if you can’t connect to a database you can’t execute a Prepared Statement. It will return an open Connection object for our Prepared Statement to use. The SERVER and DATABASE constants you would replace with your server address and database name, and the USER and PASS constants would obviously be your database username and password. This method requires the MySQL Connector/J JDBC driver. You can grab this at http://www.mysql.com
public Connection getConnection() {
try {
Class.forName(”org.gjt.mm.mysql.Driver”);
Properties connProp = new Properties();
connProp.put (”user”, USER);
connProp.put (”password”, PASS);
connProp.put (”useUnicode”, “false”);
Connection con = DriverManager.getConnection(”jdbc:mysql://” + SERVER + “/” + DATABASE, connProp);
return con;
}catch (ClassNotFoundException cnf){
return null;
}catch (SQLException ex) {
return null;
}
}
Notice that we first declare a variable to represent the SQL statement, and assign the SQL to it:
String sql = “SELECT journal_id, date_added, journal_text
FROM journal WHERE journal_id = ? AND user_id = ?”;
The values that we will be sending to the database are represented by “?“. Next we call our
getConnection () method to get our connection object and then define our Prepared Statement,
and then append our parameters to the Prepared Statement with these lines.
preparedStatement.setInt(1, journalId);
preparedStatement.setInt(2, userId);
Notice that you should use the correct set method depending on the data type of the field you are
assigning the value to (see References below). All that’s left to do is to execute the Prepared Statement
and to return the result set.
Reference: java.sun.com
Blogged with Flock
Posted in java | No Comments »