﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-yooli88-随笔分类-吸人精华 吐俺的糟粕 我吸（吸星大法 hoho~~~）</title><link>http://www.blogjava.net/yooli88/category/31298.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 01 Feb 2011 00:53:43 GMT</lastBuildDate><pubDate>Tue, 01 Feb 2011 00:53:43 GMT</pubDate><ttl>60</ttl><item><title>[转帖]在数组里随机取不重复的数</title><link>http://www.blogjava.net/yooli88/archive/2011/01/31/343137.html</link><dc:creator>迷茫在java的世界里</dc:creator><author>迷茫在java的世界里</author><pubDate>Mon, 31 Jan 2011 08:57:00 GMT</pubDate><guid>http://www.blogjava.net/yooli88/archive/2011/01/31/343137.html</guid><wfw:comment>http://www.blogjava.net/yooli88/comments/343137.html</wfw:comment><comments>http://www.blogjava.net/yooli88/archive/2011/01/31/343137.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/yooli88/comments/commentRss/343137.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/yooli88/services/trackbacks/343137.html</trackback:ping><description><![CDATA[<span  style="font-family: verdana, arial, helvetica; font-size: 12px; ">
<div>方法没有确认</div>
import java.util.*;</span>
<div><span  style="font-family: verdana, arial, helvetica; font-size: 12px; ">public class Lottery {</span>
<div><span  style="font-family: verdana, arial, helvetica; font-size: 12px; "><br />
&nbsp; &nbsp; public static void main(String[] args) {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;int[] data1 = {3, 5, 6, 8, 9, 15, 18, 24, 27, 30, 32};<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Random r = new Random();<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;int irdm = 0;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;for(int i = 0; i &lt; 7; i ++) {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;irdm = r.nextInt(11 - i);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;System.out.println(data1[irdm]);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;for(int j = irdm; j &lt; 11 - i - 1; j ++) {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; data1[j] = data1[j + 1];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
&nbsp; &nbsp; }<br />
}</span>
<div><span  style="font-family: verdana, arial, helvetica; font-size: 12px; ">方法没有确认不知道是否可行public class AAAAA {<br />
&nbsp; &nbsp; &nbsp; &nbsp; public static void main(String[] args) {<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int i = 0;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int j = 0;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] temp = new int[20];<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (j = 0; j &lt; temp.length; j++) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp[j] = Math.random() * 100&nbsp;&nbsp;+ 1;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(temp[j] + ","<img src="http://www.itpub.net/images/smilies/23.gif" smilieid="206" border="0" alt="" />;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HashSet hh = new HashSet();<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (hh.size() &lt; 7) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int aa = (int) (Math.random() * 100 + 1);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hh.add(aa);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(hh.size());<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Iterator ii = hh.iterator();<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (ii.hasNext()) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(ii.next() + ","<img src="http://www.itpub.net/images/smilies/23.gif" smilieid="206" border="0" alt="" />;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
<br />
}</span></div>
<div><span  style="font-family: verdana, arial, helvetica; font-size: 12px; "><br />
</span></div>
<div><span  style="font-family: verdana, arial, helvetica; font-size: 12px; ">java从指定数组中取不重复的7个随机数<br />
<br />
<div id="message5904759" class="t_msgfont" style="font-size: 9pt; ">如何用java从指定数组中取不重复的7个随机数，以下是我写的代码，但是是有重复的，哪位大侠赐教一下如何使用Random类的种子，帮我实现不重复的随机数。。。<br />
import java.util.*;<br />
public class Lottery {<br />
<br />
&nbsp; &nbsp; public static void main(String[] args)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;int[] data1 = {3,5,6,8,9,15,18,24,27,30,32};<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Random&nbsp; &nbsp;r=new&nbsp; &nbsp;Random();<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;StringBuffer&nbsp; &nbsp;str1=new&nbsp; &nbsp;StringBuffer();<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;for(int&nbsp; &nbsp;i=0;i&lt;6;i++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;str1.append(data1[r.nextInt(11)]);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;System.out.println(str1);<br />
&nbsp; &nbsp;}<br />
}</div>
</span></div>
</div>
</div>
  <img src ="http://www.blogjava.net/yooli88/aggbug/343137.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/yooli88/" target="_blank">迷茫在java的世界里</a> 2011-01-31 16:57 <a href="http://www.blogjava.net/yooli88/archive/2011/01/31/343137.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>