【问题标题】:get random no between range without duplicate在没有重复的范围内获得随机数
【发布时间】:2012-10-10 01:33:58
【问题描述】:

全部 我需要逻辑帮助。我在数据库中有 100 行,并随机获取所有行,没有重复我在下面的代码中使用过。

public ArrayList<Rows> getRows(){
    ArrayList<Rows> myRaw=new ArrayList<Rows>();
    Cursor rawCursor=this.db.query(DB_TABLE, null, null, null, null, null, null);
    if(rawCursor.getCount()>0){
        Random rng = new Random();
        rawCursor.moveToFirst();
        do {

             // Ideally just create one instance globally
            ArrayList<Rows> generated = new ArrayList<Raws>();
            for (int i = 0; i < 371; i++)
            {
                while(true)
                {
                    Integer next = rng.nextInt(371) + 1;
                    rawCursor.moveToPosition(next);
                    Rows raw=new Raws(rawCursor.getInt(rawCursor.getColumnIndex("_id")),rawCursor.getString(rawCursor.getColumnIndex("raw")),rawCursor.getInt(rawCursor.getColumnIndex("fav")));
                    if (!generated.contains(raw))
                    {
                        // Done for this iteration
                        generated.add(raw);
                        break;
                    }
                }
            }

            myRaw=generated;

        } while (rawCursor.moveToNext());
        rawCursor.close();
    }
    return myRaw;
}

谢谢大家

【问题讨论】:

  • 您的问题是什么?即你写的代码有什么问题?
  • 有什么理由不使用ORDER BY RANDOM()
  • @RenatoLochetti :好的,但当我提出问题并且我没有得到正确的答案时,我怎么能接受它,因为其他开发人员可能会关注它。
  • @MattWhipple 你能解释更多关于随机订单的信息吗?
  • 如果您在原始查询中添加ORDER BY RANDOM() 子句,则结果集将是随机的,您可以对其进行迭代。您将把责任转移到 SQLite 上,它的内部优化超出了您的能力范围。接受的答案会解决这个问题。

标签: android sql sqlite random


【解决方案1】:

您可以通过random number 告诉数据库对记录进行排序:

cursor = db.query(DB_TABLE, null, null, null, null, null, "random()");

【讨论】:

    【解决方案2】:

    集合洗牌怎么样? http://developer.android.com/reference/java/util/Collections.html#shuffle(java.util.List)

    public ArrayList<Rows> getRows() {
        ArrayList<Rows> myRaw = new ArrayList<Rows>();
        Cursor rawCursor = this.db.query(DB_TABLE, null, null, null, null, null, null);
    
        if (rawCursor.getCount() > 0) {
            // get all the rows
            rawCursor.moveToFirst();
            do {
                Rows raw = new Raws(rawCursor.getInt(rawCursor.getColumnIndex("_id")), rawCursor.getString(rawCursor.getColumnIndex("raw")), rawCursor.getInt(rawCursor.getColumnIndex("fav")));
                myRaw.add(raw);
            } while (rawCursor.moveToNext());
            rawCursor.close();
        }
        // shuffle the rows in some kind of random order
        Collections.shuffle(myRaw);
        return myRaw;
    }
    

    @CL 提供的答案。可能是一个更好的选择,但主要区别在于shuffle 方法可以选择提供您自己的Random 对象,这意味着您可以seed 随机。

    //Random with seed;
    Random r = new Random(68498493);
    Collections.shuffle(myRaw, r);
    

    【讨论】:

      猜你喜欢
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      • 2012-01-04
      • 2017-02-13
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      相关资源
      最近更新 更多