首页 \ 问答 \ 随机化方法的问题(Issue with randomize method)

随机化方法的问题(Issue with randomize method)

我有一个方法无法正常工作。 该方法应该随机地将一组数字从1到20排序(每个数字必须只出现一次)。 我的问题是,当我运行程序时,一些数字会重复多次。 代码如下:

public static int randomize(int index) {

    //This array will hold the 20 numbers.
    int[] randomIndex = new int[20];

    Random ranNum = new Random();

    for (int x = 0; x<20; x++) {
        int temp;

        //The number is generated randomly and saved in temp.
        temp = ranNum.nextInt(20);

        //This loop skips the first index.
        if (x != 0){

            /*Here, the loop is supposed to compare a generated number with
            the previous one*/
            for (int y = 1; y<=x; y++) {


                while(temp == randomIndex[x-y] ) {

                    /*If the while loop finds that temp variable matches any previous                          
                    number it will generate another random number for it until it finds
                    no matches.*/
                    temp = ranNum.nextInt(20);

                }  
            }
        }

    /*Once no match has been found for temp, the number is assigned to an index, 
    and the loop is executed with a x variable increment.
    randomIndex[x] = temp;

    }
   //Finally the array with the set of random numbers is sent to the main function.
   return randomIndex[index];

  }

我得到以下输出:

19, 19, 5, 16, 6, 2, 18, 1, 15, 1, 5, 19, 11, 4, 18, 0, 5, 18, 10.

所以现在我不知道该怎么做。 :C


I have a method that is not working properly. The method is supposed to sort a set of numbers from 1 to 20 randomly (each number must appear just once). My issue here is that when I run the program, some numbers are repeated several times. The code is the following:

public static int randomize(int index) {

    //This array will hold the 20 numbers.
    int[] randomIndex = new int[20];

    Random ranNum = new Random();

    for (int x = 0; x<20; x++) {
        int temp;

        //The number is generated randomly and saved in temp.
        temp = ranNum.nextInt(20);

        //This loop skips the first index.
        if (x != 0){

            /*Here, the loop is supposed to compare a generated number with
            the previous one*/
            for (int y = 1; y<=x; y++) {


                while(temp == randomIndex[x-y] ) {

                    /*If the while loop finds that temp variable matches any previous                          
                    number it will generate another random number for it until it finds
                    no matches.*/
                    temp = ranNum.nextInt(20);

                }  
            }
        }

    /*Once no match has been found for temp, the number is assigned to an index, 
    and the loop is executed with a x variable increment.
    randomIndex[x] = temp;

    }
   //Finally the array with the set of random numbers is sent to the main function.
   return randomIndex[index];

  }

And I got the following output:

19, 19, 5, 16, 6, 2, 18, 1, 15, 1, 5, 19, 11, 4, 18, 0, 5, 18, 10.

So now I have no idea what to do. :C


原文:https://stackoverflow.com/questions/21419911
更新时间:2023-09-03 17:09

最满意答案

有一个功能VB6类用于执行此操作:

ResDecomp类反编译RES文件

包含示例程序,其中一个是“查看器”,另一个只是将RT_STRING资源字符串提取到XML文档。 您可以轻松地更改后者以将字符串值转储到文本文件,数据库等。


There is a functional VB6 Class for doing this at:

ResDecomp Class Decompiles RES Files

Sample programs are included, one of them a sort of "viewer" and the other just extracts RT_STRING resource strings to an XML document. You could easily change the latter to dump the string values to a text file, database, etc. instead.

相关问答

更多
  • .Net平台引入了很多先进的技术,导致了VB6和VB.Net之间产生了一个巨大的鸿沟 就语言功能上来讲,VB.net完全是为了.Net技术而生,具有更好的运行效率和更加强大的功能(某些方面甚至超过了C#) 从语法角度上来讲,VB.net成为了真正的OOP(面向对象语言),不但支持类的继承、派生,还支持委托、泛型等等更加高级的编程技术 如果你是从VB6升级到.net的话,建议要将VB.net当作一门新的语言来学。以VB6的经验来应用到VB.net上是会犯很多错误的 关于从VB6升级到VB.net的代 ...
  • .Net平台引入了很多先进的技术,导致了VB6和VB.Net之间产生了一个巨大的鸿沟 就语言功能上来讲,VB.net完全是为了.Net技术而生,具有更好的运行效率和更加强大的功能(某些方面甚至超过了C#) 从语法角度上来讲,VB.net成为了真正的OOP(面向对象语言),不但支持类的继承、派生,还支持委托、泛型等等更加高级的编程技术 如果你是从VB6升级到.net的话,建议要将VB.net当作一门新的语言来学。以VB6的经验来应用到VB.net上是会犯很多错误的 关于从VB6升级到VB.net的代码变换问题 ...
  • 有一个功能VB6类用于执行此操作: ResDecomp类反编译RES文件 包含示例程序,其中一个是“查看器”,另一个只是将RT_STRING资源字符串提取到XML文档。 您可以轻松地更改后者以将字符串值转储到文本文件,数据库等。 There is a functional VB6 Class for doing this at: ResDecomp Class Decompiles RES Files Sample programs are included, one of them a sort of " ...
  • 有一个来自vb.net。 当您打开.vbp vb6项目时,它会询问您是否想自动将其转换为vb.net。 它做得不错,不包括第三方附加组件。 There is one that comes with vb.net. When you open a .vbp vb6 project, it asks if you would like to automatically convert it to vb.net. It does a pretty good job, exclusive of third part ...
  • 这是在VB6中逐行加载整个文件的最快方法: Function FileText (filename$) As String Dim handle As Integer handle = FreeFile Open filename$ For Input As #handle FileText = Input$(LOF(handle), handle) Close #handle End Function Public Function ReadFileIntoStr ...
  • Open strFileName For Output As #FileHandle% MousePointer = vbHourglass Print #FileHandle%, strText MousePointer = vbDefault Close #FileHandle% 转换为VB.NET代码: Me.Cursor = Cursors.WaitCursor Using fs As New StreamWriter(strFileName, Fa ...
  • 触发器是正确的vbNull是一个常量,但它的值是这样定义的: Public Const vbNull As VariantType = VariantType.Null 请参阅Microsoft参考源 Trigger is correct vbNull is a constant, but its value is defined thusly: Public Const vbNull As VariantType = VariantType.Null Please see Microsoft Refere ...
  • 作为参考: http : //technet.microsoft.com/en-us/sysinternals/bb897439.aspx Thanks all. Great ideas. Really helped me think. Upvotes all around. Ended up I didn't need to be very sure that they were strings so I went with a quick, sloppy, ugly, hack. 'strip out ...
  • 那么,你是一个接一个读一个字节吗? 在这种情况下,你迭代了200万个元素,而不是一次只取一块数据并将其写入流中。 Well, are you reading and writing each byte 1 by one? In that case you are iterating through 2 million elements instead of just taking a chunk of data at a time and write it to the stream.

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。