成都创新互联网站制作重庆分公司

java随机学码代码 java 随机算法

Java随机数代码怎么写

Math.random()是令系统随机选取大于等于 0.0 且小于 1.0 的伪随机 double 值,是Java语言常用代码。

公司主营业务:成都网站设计、成都做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出宜秀免费做网站回馈大家。

例如:var a:Number=Math.random()*2+1,设置一个随机1到3的变量。

用java随机生成四位验证码,只求编程代码

我自己做的系统里面用作验证码的JSP的

%@page contentType="image/jpeg;charset=utf-8"%

%@page import="java.util.*,java.awt.*,java.awt.image.*,javax.imageio.*" %

%@ page import="java.io.OutputStream" %

html

body

%!

Color getRandColor(int fc,int bc)

{

Random rd=new Random();

if(fc255) fc=255;

if(bc255) bc=255;

int red=fc+rd.nextInt(bc-fc);

int green=fc+rd.nextInt(bc-fc);

int blue=fc+rd.nextInt(bc-fc);

return new Color(red,green,blue);

}

%

%

Random r=new Random();

response.addHeader("Pragma","No-cache");

response.addHeader("Cache-Control","no-cache");

response.addDateHeader("expires",0);

int width=90;

int height=23;

BufferedImage pic=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

Graphics gc=pic.getGraphics();

gc.setColor(getRandColor(200,250));

gc.fillRect(0,0,width,height);

String[] rNum ={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f",

"g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w",

"x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N",

"O","P","Q","R","S","T","U","V","W","X","Y","Z"};

int[] style = {Font.PLAIN,Font.BOLD,Font.ITALIC,Font.PLAIN+Font.BOLD,

Font.BOLD+Font.ITALIC,Font.PLAIN+Font.ITALIC,Font.PLAIN+Font.BOLD+Font.ITALIC};

gc.setColor(Color.WHITE);

gc.drawLine(0,30,90,10);

gc.setColor(getRandColor(160,200));

for (int i=0;i50;i++)

{

int x = r.nextInt(width);

int y = r.nextInt(height);

int xl = r.nextInt(10);

int yl = r.nextInt(10);

gc.drawLine(x,y,x+xl,y+yl);

}

gc.setColor(getRandColor(60,150));

String rt = "";

for(int i=0;i4;i++){

String temp = rNum[r.nextInt(62)];

rt = rt+temp;

gc.setFont(new Font("Times New Roman",style[r.nextInt(7)],15));

gc.drawString(temp,5+i*15+r.nextInt(10),10+r.nextInt(10));

}

gc.dispose();

session.setAttribute("randNum",rt);

OutputStream os=response.getOutputStream();

ImageIO.write(pic,"JPEG",os);

System.out.println("当前验证码为:"+session.getAttribute("randNum"));

os.flush();

os.close();

os=null;

response.flushBuffer();

out.clear();

out = pageContext.pushBody();

%

/body

/html

如何编写一个随机出学号的Java程序

import java.util.*;

public class RandomStudentId{

private int studentID;

private Random rand = new Random();

//private Set s = new HashSet();

public void RandID(){

studentID = rand.nextInt(1000); //这里写多少,就是从0到多少的随机数字

}

public int getID(){

return studentID;

}

public String toString(){

return "Student ID: " + studentID;

}

public static void main(String[] args){

RandomStudentId rsi = new RandomStudentId();

for(int i=0; i100; ++i){

rsi.RandID();

System.out.println("NO." + rsi.getID());

}

}

}

java 假设一个班级共35人, 学号从1~35, 请编写程序进行随机学号抽取, 每个学号不

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

public class SelectNo {

public static void main(String[] args) {

//将1-35号码放入List集合

ListString list = new ArrayListString();

for (int i = 1; i  36; i++) {

list.add(""+i);

}

//新建一个List集合用来存放抽出来的号码

ListString newList = new ArrayListString();

//随机抽取list集合中的一个元素,抽出后删除

Random rd = new Random();

int count = list.size();//集合中剩余号码

while (count  0) {

int index = rd.nextInt(count);//抽出的号码的位置

//放入新的集合

newList.add(list.get(index));

//删除原有集合的元素

list.remove(index);

count--;//原有集合少了一个

}

//输出号码

for (int i = 0; i  newList.size(); i++) {

System.out.print(newList.get(i)+"\t");

//每五个换行

if(i%5 == 4){

System.out.println("");

}

}

}

}


网页名称:java随机学码代码 java 随机算法
链接分享:http://cxhlcq.com/article/dopcodc.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部