Android 如何生成单个随机值而不重复?

Android 如何生成单个随机值而不重复?,android,random,Android,Random,我正在做测验应用程序。在本应用程序中,问题不会重复。所以我使用了int value=random.nextInt(10-1)+1这样的代码。当我提交答案时,随机数将生成新的随机数,因此会生成重复的随机数。如何每次都将以前的随机值与新的随机值进行比较?将值存储在hashmap中,然后检查它是否已经存在。如果有重新滚动 将值存储在hashmap中,然后检查它是否已经存在。如果有重新滚动 从1到10生成并存储在列表中 洗牌生成的数字列表 继续从列表中删除 List List=newlinkedlist

我正在做测验应用程序。在本应用程序中,问题不会重复。所以我使用了int value=random.nextInt(10-1)+1这样的代码。当我提交答案时,随机数将生成新的随机数,因此会生成重复的随机数。如何每次都将以前的随机值与新的随机值进行比较?

将值存储在hashmap中,然后检查它是否已经存在。如果有重新滚动

将值存储在hashmap中,然后检查它是否已经存在。如果有重新滚动

  • 从1到10生成并存储在列表中
  • 洗牌生成的数字列表
  • 继续从列表中删除

    List List=newlinkedlist()

    for(int i=1;i
    
  • 从1到10生成并存储在列表中
  • 洗牌生成的数字列表
  • 继续从列表中删除

    List List=newlinkedlist();


    for(int i=1;i在该类的主要属性中使用'HashSet'类,因为它们包含一组不同的值,这意味着其中没有重复的值。。。。。。
    所以你可以生成随机编号,然后像这样把它加到集合中

    Random r = new Random();
    int i = r.nextInt(100);
    
    HashSet<int> s = new HashSet<int>();
    s.add(i);
    
    Random r=new Random();
    int i=r.nextInt(100);
    HashSet s=新的HashSet();
    s、 加(i);
    
    生成随机数并将其添加到hashset中并使用它。。。。 in-nextInt参数必须给出最大编号范围

    示例代码如下所示:

    Random r = new Random();
            //declare a hash set
            HashSet set = new HashSet();
    
    
            for(int i=0;i<50;i++)
            {
                set.add(r.nextInt(100));
            }
    
    
             // create an iterator
              Iterator iterator = set.iterator(); 
    
              // check values
              while (iterator.hasNext()){
                 System.out.println("Value: "+iterator.next() + " ");  
              }
    
    Random r=new Random();
    //声明哈希集
    HashSet=newhashset();
    
    对于(int i=0;i在该类的主要属性中使用'HashSet'类,因为它们包含一组不同的值,这意味着其中没有重复的值。。。。。。 所以你可以生成随机编号,然后像这样把它加到集合中

    Random r = new Random();
    int i = r.nextInt(100);
    
    HashSet<int> s = new HashSet<int>();
    s.add(i);
    
    Random r=new Random();
    int i=r.nextInt(100);
    HashSet s=新的HashSet();
    s、 加(i);
    
    生成随机数并将其添加到hashset中并使用它。。。。 in-nextInt参数必须给出最大编号范围

    示例代码如下所示:

    Random r = new Random();
            //declare a hash set
            HashSet set = new HashSet();
    
    
            for(int i=0;i<50;i++)
            {
                set.add(r.nextInt(100));
            }
    
    
             // create an iterator
              Iterator iterator = set.iterator(); 
    
              // check values
              while (iterator.hasNext()){
                 System.out.println("Value: "+iterator.next() + " ");  
              }
    
    Random r=new Random();
    //声明哈希集
    HashSet=newhashset();
    
    对于(inti=0;i,这里是我在项目中使用的代码

    package com.banglardin.test\u代码;
    导入android.app.*;
    导入android.content.*;
    导入android.content.res.*;
    导入android.os.*;
    导入android.view.*;
    导入android.widget.*;
    导入com.banglardin.test_代码。*;
    导入java.util.*;
    公共类MainActivity扩展活动{
    受保护的共享参考优先权;
    保护对象;
    受保护的文本视图文本视图;
    保护按钮视图、清洁按钮;
    受保护的数组列表ques_数组;
    受保护的最终字符串KEY=“Key124”;
    受保护整数i=0;
    /**在首次创建活动时调用*/
    @凌驾
    创建时的公共void(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    requestWindowFeature(窗口。功能\u无\u标题);
    setContentView(R.layout.main);
    //初始问题与偏好
    questionObject=新问题();
    preference=GetSharedReferences(键,Context.MODE\u WORLD\u WRITEABLE);
    //从问题对象获取数组
    试试{
    ques_array=questionObject.getQestions(getApplicationContext());
    }捕获(例外e){
    e、 printStackTrace();
    }
    //初始化视图
    textView=(textView)findViewById(R.id.question);
    buttonView=(按钮)findViewById(R.id.Button);
    cleanButton=(按钮)findViewById(R.id.Button\u clean);
    textView.setTextSize(18.33f);
    按钮视图设置尺寸(18.00f);
    cleanButton.SettexSize(18.00f);
    //在按钮视图上设置onclickListener
    buttonView.setOnClickListener(新视图.OnClickListener(){
    公共void onClick(视图v){
    int set=0;
    如果(i<6){
    while(set==0){
    字符串历史记录=获取字符串(键);//
    随机r=新随机();
    int id=r.nextInt(ques_array.size());
    字符串s_id=”“;//例如:
    如果(!history.contains(s_id)){
    setText(ques_array.get(id));
    setString(KEY,(history+s_id));//ex:+=;
    set=67;
    i++;
    }          
    }
    }
    如果(i>=6),则为else{
    setText(getResources().getString(R.string.e2));
    Toast.makeText(MainActivity.this,“问题不再可用”,2.show();
    }           
    }         
    }
    );
    //在按钮视图上设置onclickListener
    cleanButton.setOnClickListener(新视图.OnClickListener(){
    公共void onClick(视图v){
    设置字符串(键“”);
    }
    }
    );
    }
    @凌驾
    public void onBackPressed(){
    if(首选项!=null){
    设置字符串(键,(“”);
    完成();
    }   
    super.onBackPressed();
    }
    /**从首选项获取字符串值*/
    私有字符串getString(字符串键){
    if(首选项!=null){
    返回preference.getString(键“”);
    }
    否则{
    返回null;
    }
    }
    /**将字符串值设置为首选项*/
    私有void setString(字符串键,字符串值){
    if(首选项!=null){
    SharedReferences.Editor edit=preference.edit();
    编辑.putString(键、值);
    edit.commit();
    }
    }
    /**给我们所有问题的课堂*/
    课堂提问{
    受保护的ArrayList数据;
    公共ArrayList getQestions(上下文c)引发异常{
    数据=新的ArrayList();
    Resources res=c.getResources();
    字符串qes[]={
    res.getString(R.string.q1),//0
    res.getString(R.string.q2),//1
    res.getString(R.string.q3),//2
    res.getString(R.string.q4),//3
    res.getString(R.string.q5),//4
    res.getString(R.string.q6),//5
    res.getString(R.string.q7),//6
    };
    //逐个添加所有字符串
    用于(字符串i:qes){
    数据.添加(i);
    }
    返回数据;
    }           
    }
    }
    
    以下是我在项目中使用的代码。完整的源代码是

    package com.banglardin.test\u代码;
    导入android.app.*;
    导入android.content.*;
    导入android.content.res.*;
    导入android.os