Java SQLite中的随机数据
我请求您帮助解决我的android应用程序中的问题。我有一个项目要做一个测验应用程序,我想在这个游戏中实现线性同余方法。我成功地随机分配了数据,但我仍然不知道如何实现该方法。这是我的密码Java SQLite中的随机数据,java,android,sqlite,Java,Android,Sqlite,我请求您帮助解决我的android应用程序中的问题。我有一个项目要做一个测验应用程序,我想在这个游戏中实现线性同余方法。我成功地随机分配了数据,但我仍然不知道如何实现该方法。这是我的密码 public class QuizHelper extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; // Database Name private static final String D
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;
}
}
public类QuizHelper扩展了SQLiteOpenHelper{
私有静态最终int数据库_VERSION=1;
//数据库名称
私有静态最终字符串数据库\u NAME=“mathsone”;
//任务表名称
私有静态最终字符串表_QUEST=“QUEST”;
//任务表列名称
私有静态最终字符串密钥\u ID=“qid”;
私有静态最终字符串KEY_QUES=“question”;
私有静态最终字符串键\u ANSWER=“ANSWER”//正确选项
私有静态最终字符串键\u OPTA=“OPTA”//选项a
私有静态最终字符串键\u OPTB=“OPTB”//选项b
私有静态最终字符串键\u OPTC=“OPTC”//选项c
私有静态最终字符串密钥\u OPTD=“OPTD”;
专用数据库数据库;
公共QuizHelper(上下文){
super(上下文、数据库名称、null、数据库版本);
}
@凌驾
public void onCreate(SQLiteDatabase db){
dbase=db;
String sql=“创建不存在的表格”+表格任务+”(“
+密钥ID+“整数主键自动递增”+“密钥”
+“文本,”+按键+答案+“文本,”+按键+选项+文本,”
+按键OPTB+“文本”+“按键OPTC+“文本”+“按键OPTD+“文本”;
execSQL(sql);
添加问题();
//db.close();
}
私人问题{
问题q1=新问题(“5+2=?”、“7”、“8”、“6”、“4”、“7”);
这个问题(q1);
问题q2=新问题(“2+18=?”、“18”、“19”、“20”、“4”、“20”);
这个问题(q2);
问题q3=新问题(“10-3=?”、“6”、“7”、“8”、“5”、“7”);
这个问题(第三季度);
问题q4=新问题(“5+7=?”、“12”、“13”、“14”、“5”、“12”);
这个问题(第四季度);
问题q5=新问题(“3-1=?”、“1”、“3”、“2”、“5”、“2”);
这个问题(q5);
问题q6=新问题(“0+1=?”、“1”、“0”、“10”、“5”、“1”);
这个问题(q6);
问题q7=新问题(“9-9=?”、“0”、“9”、“1”、“5”、“0”);
这个问题(q7);
问题q8=新问题(“3+6=?”、“8”、“7”、“9”、“5”、“9”);
这个问题(q8);
问题q9=新问题(“1+5=?”、“6”、“7”、“5”、“5”、“6”);
这个问题(q9);
问题q10=新问题(“7-5=?”、“3”、“2”、“6”、“5”、“2”);
本.补充质询(q10);
//结束
}
@凌驾
public void onUpgrade(SQLiteDatabase db、intoldv、intnewv){
//删除旧表(如果存在)
db.execSQL(“如果存在删除表”+表任务);
//再次创建表
onCreate(db);
}
//添加新问题
公共问题(问题探索){
//SQLiteDatabase db=this.getWritableDatabase();
ContentValues=新的ContentValues();
value.put(KEY_QUES,quest.getQUESTION());
value.put(KEY_ANSWER,quest.getANSWER());
value.put(KEY_OPTA,quest.getOPTA());
value.put(KEY_OPTB,quest.getOPTB());
value.put(KEY_OPTC,quest.getOPTC());
value.put(KEY_OPTD,quest.getOPTD());
//插入行
insert(TABLE_QUEST,null,value);
}
公共列表getAllQuestions(){
List quesList=new ArrayList();
//选择所有查询
String selectQuery=“SELECT*FROM”+TABLE_QUEST+“ORDER BY RANDOM(),null”;
dbase=this.getReadableDatabase();
Cursor Cursor=dbase.rawQuery(selectQuery,null);
//循环遍历所有行并添加到列表
if(cursor.moveToFirst()){
做{
问题探索=新问题();
setID(cursor.getInt(0));
setquest.setQUESTION(cursor.getString(1));
setANSWER(cursor.getString(2));
setOPTA(cursor.getString(3));
setOPTB(cursor.getString(4));
setOPTC(cursor.getString(5));
setOPTD(cursor.getString(6));
添加(任务);
}while(cursor.moveToNext());
}
//返回任务列表
返回任务列表;
}
}
你可以在这里找到公式
SQlite Random返回-9223372036854775808和+9223372036854775807之间的伪随机整数 如果您使用android提供的random(),应该可以解决您的问题 随机类使用48位种子,该种子使用线性同余公式修改。 换行
String selectQuery = "SELECT * FROM " + TABLE_QUEST + " ORDER BY RANDOM(), null";
到
什么方法?请清楚地说明你的问题。我想用线性同余法随机回答这个问题。这里是你必须根据这个公式开发一个算法的url,然后在你的代码中使用这个算法,我相信你可以在网上找到这个方法的算法。简化你的生活:只需添加
。。。随机排序
到您的查询中。其代码与此相同吗?r(n+1)=a×rn+c(mod m),其中:r0是种子。r1,r2,r3,…,是随机数。a,m是常数,它将导致x(1)=(11(1)+7)模50=18x(2)=(11(18)+7)模50=5x(3)=(11(5)+7)模50=12x(4)=(11(12)+7)模50=39x(5)=(11(39)+7)模50=36x(6)=(11(36)+7)模50=3x(7)=(11(3)+7)模50=40x(8)=(11(40)+7)模50=47x(9)=(11(47)+7)模50=24x(10)=(11(24)+7)模50=21x(11)=(11(21)+7)模50=38x(12)=(11(38)+7)模50=25
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";