向各位高人请教能生成六位数字随机数的方法?

ervinlj 2003-10-17 01:18:20
各位,大家好:
我想请教一下如何写一个方法来生成六位数字的随机数?这个方法应该如何来实现啊?马上给分的!!!
...全文
455 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongshaojie 2003-10-17
  • 打赏
  • 举报
回复
public int getNum(){
int n = 0;
n = (int)(Math.random() * (999999 - 100000));
n = n + 100000;
return n;
}
fft123 2003-10-17
  • 打赏
  • 举报
回复
long l = (long)(Math.random()*1000000);
要6位,乘以1000000(1后面带6个0)
要其它的位数,同上
ervinlj 2003-10-17
  • 打赏
  • 举报
回复
up
ervinlj 2003-10-17
  • 打赏
  • 举报
回复
各位看我最后一个问题怎样来解决呢?马上给分啊!!!急,
提示说Random类找不到啊!!!
ervinlj 2003-10-17
  • 打赏
  • 举报
回复
不是啊,还是不明白!
我写了这样一段代码:
<%
String s="";
int intCount=0;
intCount=(new Random()).nextInt(999999);//
if(intCount<100000)intCount+=100000;
s=intCount+"";
out.println("Random number is:"+s);
%>
我的意思是在每次调用这个jsp页面的时候,让这个jsp页面输出一个000001-999999的随机数应该怎样来做?
aku0708 2003-10-17
  • 打赏
  • 举报
回复
在JSP页面中:
。。。
。。。
<img src="/getimg">
...
<%=session.getAttribute("getImg") %>
....
你看一看吧
aku0708 2003-10-17
  • 打赏
  • 举报
回复
不是吧

我给你个例子
package com.aku.image;

import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
import java.awt.image.*;

/**
* Title: getImg.java
* Description: 这个class主要实现随机生成一个4位数的验证码,并写入session,
* Copyright: Copyright (c) 2003
* Company:
* @author
* @version 1.1
*/

public class getImg
extends HttpServlet {

private Font mFont = new Font("宋体", Font.PLAIN, 8); //设置字体
//处理post
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {

doGet(request, response);
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
//取得一个1000-9999的随机数
String s = "";

int intCount = 0;

intCount = (new Random()).nextInt(999999); //

if (intCount < 100000) {
intCount += 100000;

}
s = intCount + "";

//对session付值。

HttpSession session = request.getSession(true);

session.setAttribute("getImg", s);

response.setContentType("image/gif");

ServletOutputStream out = response.getOutputStream();

BufferedImage image = new BufferedImage(35, 14, BufferedImage.TYPE_INT_RGB);

Graphics gra = image.getGraphics();
//设置背景色
gra.setColor(Color.yellow);

gra.fillRect(1, 1, 33, 12);
//设置字体色
gra.setColor(Color.black);

gra.setFont(mFont);
//输出数字
char c;

for (int i = 0; i < 6; i++) {

c = s.charAt(i);

gra.drawString(c + "", i * 11 + 6, 11); //7为宽度,11为上下高度位置

}
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

encoder.encode(image);

out.close();
}

}
ervinlj 2003-10-17
  • 打赏
  • 举报
回复
up
ervinlj 2003-10-17
  • 打赏
  • 举报
回复
请问楼上那位,你的new Random()这个类是那里来的?在Java的库里好象是没有这个类的吗?
还有如果我要生成000001-999999之间的随机数应该怎样来做呢?
aku0708 2003-10-17
  • 打赏
  • 举报
回复
public String getNum()
{

//取得一个100000-999999的随机数
String s="";

int intCount=0;

intCount=(new Random()).nextInt(999999);//

if(intCount<100000)intCount+=100000;

s=intCount+"";
return s;
}
aku0708 2003-10-17
  • 打赏
  • 举报
回复
//取得一个100000-999999的随机数
String s="";

int intCount=0;

intCount=(new Random()).nextInt(999999);//

if(intCount<100000)intCount+=100000;

s=intCount+"";

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧