Home » Android » Random data in SQLite

Random data in SQLite

Posted by: admin November 1, 2017 Leave a comment

Questions:

I am asking your help to fix my problem in my android applications. I have a project to make a quiz apps, and I want to implement linear congruential method in this game. I have success to random my data, but I still have no idea how to implement the method. Here is my code;

public class QuizHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    // Database Name
    private static final String DATABASE_NAME = "mathsone";
    // tasks table name
    private static final String TABLE_QUEST = "quest";
    // tasks Table Columns names
    private static final String KEY_ID = "qid";
    private static final String KEY_QUES = "question";
    private static final String KEY_ANSWER = "answer"; // correct option
    private static final String KEY_OPTA = "opta"; // option a
    private static final String KEY_OPTB = "optb"; // option b
    private static final String KEY_OPTC = "optc" ; // option c
    private static final String KEY_OPTD = "optd" ;

    private SQLiteDatabase dbase;

    public QuizHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        dbase = db;
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
                + " TEXT, " + KEY_ANSWER + " TEXT, " + KEY_OPTA + " TEXT, "
                + KEY_OPTB + " TEXT, " + KEY_OPTC + " TEXT, "+ KEY_OPTD + " TEXT)";
        db.execSQL(sql);
        addQuestion();
        // db.close();

    }

    private void addQuestion() {
        Question q1 = new Question("5+2 = ?", "7", "8", "6", "4", "7");
        this.addQuestion(q1);
        Question q2 = new Question("2+18 = ?", "18", "19", "20", "4", "20");
        this.addQuestion(q2);
        Question q3 = new Question("10-3 = ?", "6", "7", "8", "5", "7");
        this.addQuestion(q3);
        Question q4 = new Question("5+7 = ?", "12", "13", "14", "5", "12");
        this.addQuestion(q4);
        Question q5 = new Question("3-1 = ?", "1", "3", "2", "5", "2");
        this.addQuestion(q5);
        Question q6 = new Question("0+1 = ?", "1", "0", "10", "5", "1");
        this.addQuestion(q6);
        Question q7 = new Question("9-9 = ?", "0", "9", "1", "5", "0");
        this.addQuestion(q7);
        Question q8 = new Question("3+6 = ?", "8", "7", "9", "5", "9");
        this.addQuestion(q8);
        Question q9 = new Question("1+5 = ?", "6", "7", "5", "5", "6");
        this.addQuestion(q9);
        Question q10 = new Question("7-5 = ?", "3", "2", "6", "5", "2");
        this.addQuestion(q10);

        // END
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);

        // Create tables again
        onCreate(db);
    }

    // Adding new question
    public void addQuestion(Question quest) {
        // SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_QUES, quest.getQUESTION());
        values.put(KEY_ANSWER, quest.getANSWER());
        values.put(KEY_OPTA, quest.getOPTA());
        values.put(KEY_OPTB, quest.getOPTB());
        values.put(KEY_OPTC, quest.getOPTC());
        values.put(KEY_OPTD, quest.getOPTD());

        // Inserting Row
        dbase.insert(TABLE_QUEST, null, values);
    }

    public List<Question> getAllQuestions() {
        List<Question> quesList = new ArrayList<Question>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_QUEST  + " ORDER BY RANDOM(), null";
        dbase = this.getReadableDatabase();
        Cursor cursor = dbase.rawQuery(selectQuery, null);
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Question quest = new Question();
                quest.setID(cursor.getInt(0));
                quest.setQUESTION(cursor.getString(1));
                quest.setANSWER(cursor.getString(2));
                quest.setOPTA(cursor.getString(3));
                quest.setOPTB(cursor.getString(4));
                quest.setOPTC(cursor.getString(5));
                quest.setOPTD(cursor.getString(6));

                quesList.add(quest);
            } while (cursor.moveToNext());
        }
        // return quest list
        return quesList;
    }

}

you can find the formula here

https://id.wikipedia.org/wiki/Metode_linear_kongruen

Answers:

SQlite Random return pseudo-random integer between -9223372036854775808 and +9223372036854775807.

https://sqlite.org/lang_corefunc.html#randomblob

If you use random() given by android that should solve your problem

Random class uses a 48-bit seed, which is modified using a linear congruential formula.
https://developer.android.com/reference/java/util/Random.html

Change this line

String selectQuery = "SELECT  * FROM " + TABLE_QUEST  + " ORDER BY RANDOM(), null";

to

Random r = new Random();
int i1 = r.nextInt(MAX_Database_row_Count - Min_Database_row_count) + Min_Database_row_count;
String selectQuery = "SELECT  * FROM " + TABLE_QUEST  + " ORDER BY "+Integer.toString(i1)+", null";