关闭

一个随机数算法

279人阅读 评论(0) 收藏 举报
 
 question:
suppose give a function Randomnized(1,N), it can generate from 1 to N random Integer number, pls use this function to accomplish a most effective program, it can generate 6 unduplicate nubmers.
answer:
publicclass RamdomNumber {
 
      publicstaticvoid main(String[] args) {
           
            int numbers[]=newint[50];
           
            //add values to numbers array
            for(int i=0;i<50 ;i++ )
            {
                  numbers[i] = i+1;
            }
 
            int[] luckyNumbers = newint[20];
           
            int i = 50;
 
           
            for(int j = 0; j < 20; j++)
            {
                  int index = (int)(Math.random()*(i-1));
                 
                  System.out.println(index);
                  System.out.println("*************");
                  luckyNumbers[j] = numbers[index];
                 
                  numbers[index] = numbers[numbers.length-j-1];
                 
                  i--;
            }
     
            //output the values
            for(int t = 0; t < 20 ; t++)
            {
                  System.out.println(luckyNumbers [t]);
            }
      }
 
}
 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:84566次
    • 积分:1632
    • 等级:
    • 排名:千里之外
    • 原创:76篇
    • 转载:9篇
    • 译文:0篇
    • 评论:8条
    文章分类
    最新评论