Random 我需要一个项目的数字和字母生成器

Random 我需要一个项目的数字和字母生成器,random,code-generation,new-operator,letters-and-numbers,Random,Code Generation,New Operator,Letters And Numbers,我需要数字和字母有点像0000-00-0000A。 我正在做一个项目,所以当我放置优惠券时,随机数字和字母将出现在我放置优惠券的位置的优惠券id上,但对于每个优惠券,数字和字母是不同的。 是的,我是新的编码2个月,我想我做的很好,除了这个随机数的事情。下面是我正在做的例子 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transit

我需要数字和字母有点像0000-00-0000A。 我正在做一个项目,所以当我放置优惠券时,随机数字和字母将出现在我放置优惠券的位置的优惠券id上,但对于每个优惠券,数字和字母是不同的。 是的,我是新的编码2个月,我想我做的很好,除了这个随机数的事情。下面是我正在做的例子

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
#coupon{
     width: 600px;
     height:auto;
     top:145px;
    background: #FFFF95;
    margin: 2px auto;
    border: 1px solid #000000;
    text-align: center;
    -webkit-border-top-left-radius:06px;
    -webkit-border-top-right-radius:06px;
    -webkit-border-bottom-left-radius:06px;
    -webkit-border-bottom-right-radius:06px;
    -moz-border-radius-topleft:06px;
    -moz-border-radius-bottomleft:06px;
    -moz-border-radius-topright:06px;
    -moz-border-radius-bottomright:06px;
    border-top-left-radius:06px;
    border-top-right-radius:06px;
    border-bottom-left-radius:06px;
    border-bottom-right-radius:06px;
    /* additional features, can be omitted */
    border:2px outset #093;
    padding:15px;
    font-size:15px;
    -moz-box-shadow: 0 0 15px #999;
    -webkit-box-shadow: 0 0 15px #999;
    box-shadow: 0 0 15px #999;
}


</style>
</head>

<body>
<div id="coupon" class="coupon">
<p>This can be the coupn area where the cupon is place and the deal can be read then printed!</p>
<div class="print-page" id="print-page">
  <p><a href=" #" onClick=" window.print()" >Print Deal</a></p>
</div>
<div class="coupon-id-number" id="coupon-id-number">0000-00-0000-A</div></div>

</body>
</html>

无标题文件
#息票{
宽度:600px;
高度:自动;
顶部:145px;
背景#FFFF95;
保证金:2倍自动;
边框:1px实心#000000;
文本对齐:居中;
-webkit边框左上半径:06px;
-webkit边框右上角半径:06px;
-webkit边框左下半径:06px;
-webkit边框右下半径:06px;
-左上角moz边界半径:06px;
-moz边框半径左下角:06px;
-moz边界半径右上角:06px;
-moz边界半径右下角:06px;
边框左上半径:06px;
边框右上角半径:06px;
边框左下半径:06px;
边框右下半径:06px;
/*附加功能,可以省略*/
边界:2px起点#093;
填充:15px;
字体大小:15px;
-moz盒阴影:0 15px#999;
-网络工具包盒阴影:0 15px#999;
盒影:0 0 15px#999;
}
这可以是coupn区域,在该区域内放置cupon,交易可以阅读然后打印

0000-00-0000-A

有人能帮我吗?

在java中,您可以执行以下操作:

Random random = new Random();
String couponCode = String.format("%04d-%02d-%04d-%s", random.nextInt(10000), random.nextInt(100), random.nextInt(10000), (char)(random.nextInt(26)+65));

如果您想通过JavaScript实现这一点,您可能需要一个函数,如

<script>
function genRandom() {
  var genNumber = function (length) {
        return ~~(Math.random() * length);
      },
      genChar = function () {
        var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXZ',
            index = genNumber(24);

        return chars.slice(index, index + 1);
      },
      arr = [genNumber(10000), genNumber(100), genNumber(10000), genChar()];
  return arr.join('-');
}

document.getElementById('coupon-id-number').innerHTML = genRandom();
</script>

函数genRandom(){
var genNumber=函数(长度){
返回~~(Math.random()*长度);
},
genChar=函数(){
var chars='ABCDEFGHIJKLMNOPQRSTUVWXZ',
索引=genNumber(24);
返回字符片(索引,索引+1);
},
arr=[genNumber(10000)、genNumber(100)、genNumber(10000)、genChar();
返回arr.join('-');
}
document.getElementById('coupon-id-number')。innerHTML=genRandom();

您使用哪种服务器端技术?您希望使用哪种语言?我希望您使用一个数据库来存储优惠券号码,以便以后可以对其进行验证(作为一次性使用)。。。我对任何事情都很陌生,是的,我有一个储存优惠券id的系统。Javascript是首选。你想在服务器端插入数据库并测试相同的代码是否存在,客户端只需使用Ajax从服务器中提取一个新的优惠券代码……要检查随机数是否已经是令牌,你必须在数据库中跟踪它们。