Topic: 怎么产生10个随机数付给数组 |
Print this page |
1.怎么产生10个随机数付给数组 | Copy to clipboard |
Posted by: shibin20011983 Posted on: 2005-12-12 19:03 如题:我想产生10个1-100的随机数,把产生后的10个数付给一个数组,谢谢指点。 |
2.Re:怎么产生10个随机数付给数组 [Re: shibin20011983] | Copy to clipboard |
Posted by: why Posted on: 2005-12-12 19:09 java.util.Random http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html i.e. Random rand = new Random(); int i = rand.nextInt(100) + 1; |
3.Re:怎么产生10个随机数付给数组 [Re: shibin20011983] | Copy to clipboard |
Posted by: shibin20011983 Posted on: 2005-12-12 19:51 why,谢谢,你的回答只产生一个随机数。 我想要的是10个随机数,并且付给数组。 我想知道是否一定要用for语句? thankyou! |
4.Re:怎么产生10个随机数付给数组 [Re: shibin20011983] | Copy to clipboard |
Posted by: why Posted on: 2005-12-12 20:11 是有意留給閣下動動手 本來 rand.nextInt(100) + 1 也不想寫的說 閣下看過java.util.Random的API doc沒有? 明白為何要加一嗎? 怎様"要10个随机数,付给数组"? 坦白說,敝人不太理解其意思 當然不是"一定要用for",閣下可以用while,可以寫 myIntArray[0] = rand.nextInt(100) + 1; ... myIntArray[9] = rand.nextInt(100) + 1; ... 但是,始終也是for的路數吧 |
5.Re:怎么产生10个随机数付给数组 [Re: shibin20011983] | Copy to clipboard |
Posted by: shibin20011983 Posted on: 2005-12-12 21:17 rand.nextInt(100) + 1 是1-100呀(在我问这个题目前我已经看过这个类了) 因为rand.nextInt(100)是1(包括)到100(不包括)。 如果要用循环的思路分别付值的话,我会做的。 我是想问问java里面有没有不用循环就可以直接把产出的随机数付给一个数组 ^_^ 谢谢why大哥! |
6.Re:怎么产生10个随机数付给数组 [Re: shibin20011983] | Copy to clipboard |
Posted by: why Posted on: 2005-12-12 23:22 shibin20011983 wrote: Why do you think that there would be such a method with limited use? Do you know any programming language that has this? Why do you need such a functionality? Do you think there's a method to initialize an integer array with specific values? Why would 随机数 make any differnce? |
7.Re:怎么产生10个随机数付给数组 [Re: shibin20011983] | Copy to clipboard |
Posted by: shibin20011983 Posted on: 2005-12-13 20:35 Why do you think that there would be such a method with limited use? A:因为我觉得java程序里面的api函数很多,可能就有这个功能的函数。 Do you know any programming language that has this? A:i don't know. Why do you need such a functionality? A:定义一个数组,编一个查找其中某一个数的函数。(当然可以确定里面的数,我想要随机产生。) |
8.Re:怎么产生10个随机数付给数组 [Re: shibin20011983] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2005-12-14 02:43 Dear Mr. Why: Why did you waste your precious time to discuss this nonsense... Thanks! |
9.Re:怎么产生10个随机数付给数组 [Re: shibin20011983] | Copy to clipboard |
Posted by: shibin20011983 Posted on: 2005-12-14 13:21 -_-!,gongshi,你这样好像。。。,你这个论坛不就是为了解决别人的问题吗? 虽然我的问题可能不是很好,你可以不浪费你的宝贵时间,但是你不能强迫别人回答我的问题把。 你也是从不知道到知道的呀,我现在就是你不知道的那个时候呀。 你这样,谁还敢在这里提问题呀。 谢谢,我只想说我的意思,如果你生气了,那对不起了。 |
10.Re:怎么产生10个随机数付给数组 [Re: shibin20011983] | Copy to clipboard |
Posted by: why Posted on: 2005-12-14 19:16 shibin20011983大俠 閣下可算是缺乏常識卻鑽牛角尖,這是編程的大忌 |
11.Re:怎么产生10个随机数付给数组 [Re: shibin20011983] | Copy to clipboard |
Posted by: sz_ren Posted on: 2005-12-14 19:45 楼主!!不要把自己的脑细胞白白浪费在这种已经可以用好多种方法解决的问题上了,因为这个问题很简单,你不会是要创新??唉,如果是这样,那么我无话可说! |
12.Re:怎么产生10个随机数付给数组 [Re: shibin20011983] | Copy to clipboard |
Posted by: shibin20011983 Posted on: 2005-12-14 21:46 晕,我其实只是随便问问有没有实现这个功能的函数而已,你们只要说没有,或有(怎么解决),不就可以了,好像是你们接着我的问题说了很多而已,呵呵,好了,这个问题就到此为止了,谢谢why大哥的劝告,我会注意的。 |
13.Re:怎么产生10个随机数付给数组 [Re: shibin20011983] | Copy to clipboard |
Posted by: javasons Posted on: 2005-12-14 23:38 import java.util.Random; class Number { public static void main(String args[]) { Random random= new Random(); int [] num=new int[100]; for (int i = 0; i < 100; i++) { num[i]=random.nextInt(10)+1; System.out.println(num[i]); } } } |
14.Re:怎么产生10个随机数付给数组 [Re: shibin20011983] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2005-12-15 00:36 To javasons Welcome to CJSDN! Thank you for trying to be helpful! You did not read the thread, and gave the exact correct answer other's did not want! I know this is your first post, and it is understandable. My suggestion, since you are new here, before giving answers, read a little more first, please! Thanks! |
15.Re:怎么产生10个随机数付给数组 [Re: shibin20011983] | Copy to clipboard |
Posted by: Jcat Posted on: 2005-12-21 18:45 shibin20011983 wrote: 大猫总是很严厉的,in my opinion,一个“纯净”的论坛就应该有一个这样的版主(当然也不能太多,否则就会出现你说的问题)。这也正是我喜欢这里的原因,大猫让我养成了一种很好的提问、search、答问的习惯。 如果你想实现“有问题就可以问”,这样的论坛太多了。 诚然,一种很好的提问、search、答问的习惯会比较“浪费”个人的时间。但对整个论坛是有好处的(至少可以减少重复的发问),而且对你自己也是有好处的(待久了就会体会了)。 Just personal opinion! THX~ |
16.Re:怎么产生10个随机数付给数组 [Re: shibin20011983] | Copy to clipboard |
Posted by: asus526 Posted on: 2005-12-23 16:32 class Rnd_36_7{ public static void main(string[] args){ int a[] =new int[]; for(int i=0;i<=a.length;i++){ one_num; while(true){ a[i]=(int)(math.random()*36+1); for(int j=0;j<i;j++){ if(a[i]==a[j]) continue one_num; } break; } } for(int i=0;i<a.length;i++) System.out.println(""+a[i]); System.out.println(); } } |
17.Re:怎么产生10个随机数付给数组 [Re: shibin20011983] | Copy to clipboard |
Posted by: 毒毒√快山 Posted on: 2006-01-23 17:35 靠,很简单啊,你循环10遍,不就有10个随机值拉 在 循环里,每次得到一个随机值,就把它放到数组里撒。 int array[] = new int[10]; int index = 0; for(int i=0; i<11; i++){ index = (Math.random() * 10); array[i] = index; } 应该可以吧,没试过,随手写的。错了,就不好意思拉 |
18.Re:怎么产生10个随机数付给数组 [Re: shibin20011983] | Copy to clipboard |
Posted by: 毒毒√快山 Posted on: 2006-01-23 17:36 这样写,哈。刚写错了 for(int i=1; i<11; i++){ index = (Math.random() * 10); array[i] = index; } |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |