如何生成Java Card中两个数字之间的随机数字?例如,编号应该在0到50之间生成。生成Java Card中两个数字之间的随机数字
回答
这里有examples这里。以从该链接的例子2:
import java.util.Random;
/** Generate random integers in a certain range. */
public final class RandomRange {
public static final void main(String... aArgs){
log("Generating random integers in the range 1..10.");
int START = 1;
int END = 10;
Random random = new Random();
for (int idx = 1; idx <= 10; ++idx){
showRandomInteger(START, END, random);
}
log("Done.");
}
private static void showRandomInteger(int aStart, int aEnd, Random aRandom){
if (aStart > aEnd) {
throw new IllegalArgumentException("Start cannot exceed End.");
}
//get the range, casting to long to avoid overflow problems
long range = (long)aEnd - (long)aStart + 1;
// compute a fraction of the range, 0 <= frac < range
long fraction = (long)(range * aRandom.nextDouble());
int randomNumber = (int)(fraction + aStart);
log("Generated : " + randomNumber);
}
private static void log(String aMessage){
System.out.println(aMessage);
}
}
这是在Java中,我想相同的结果在java卡 –
使用RandomData抽象类:
http://www.win.tue.nl/pinpasjc/docs/apis/jc222/javacard/security/RandomData.html
编辑:
这里是这个类中使用的的exaple,
byte[] keyBytes = JCSystem.getTransientByteArray(COD,16);
RandomData rng = RandomData.getInstance(ALG_SECURE_RANDOM);
rng.generateData(keyBytes,0,16);
如果您只是寻找随机数而不是randomData您可以使用此:
/*
* generate a random number between 0 and 50
*/
Random random1 = new Random();
int card = (random1.nextInt(51));
我看到这个链接,但我不知道如何使用这个功能,你能给我一个例子请 –
我编辑我的答案与RandomData类的例子使用 –
谢谢你的repley,我想要seconde例子的相同结果:int card =(random1.nextInt(51));但是在javaCard中,因为这个Random在java卡API中不可用2.2.2 –
在Java卡,你只能访问到javacard.security.RandomData,这是唯一能够生成随机字节。
首先需要正确类型的变量:
private RandomData rng;
private byte[] rndBuffer;
然后,你需要下面的代码在构造函数/安装(避免分配一个随机数发生器和缓冲器每次):
rng = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
rndBuffer = JCSystem.getTransientByteArray(JCSystem.CLEAR_ON_DESELECT, 1);
然后,你可以定义你的方法得到的范围内的随机字节:
byte getRandomByteInRange(byte min, byte max) {
do {
rng.generateData(rndBuffer,0,1);
while ((rndBuffer[0]<min) || (rndBuffer[0]>max))
return rndBuffer[0];
}
写这种方法很可能不会那么愚蠢(特别是对于小范围),但它应该起作用。
是的,有,检查[这个(有点数学)在加密答案](http://crypto.stackexchange.com/questions/7996/correct-way-to-map-random-number-to-defined-range) ,Java的通常答案是在Java SE中使用(或在这种情况下复制)Random.nextInt()的方法。 –
请测试您的代码。 'getTransientByteArray'不存在,参数的顺序不正确,直接对这个值进行测试是非常幼稚的。此外,我不认为最小值和最大值都应该是排他性的。通常它是从最小(含)到最大(排他)。可以说包容性的包容性,但我不认为有人会预期从0到2的范围只是能够提供值1。 –
虽然你正朝着正确的方向:) –
被这个问题所吸引,我决定再次通过StackOverflow做一些代码贡献。请注意,我仅在Java SE中使用SecureRandom
测试了随机数的分布。我相当确定这段代码是正确的。
所以没有进一步的话,代码。
package nl.owlstead.jcsecurerandom;
import javacard.framework.JCSystem;
import javacard.framework.Util;
import javacard.security.RandomData;
/**
* Generates numbers within a range. This class uses modulo arithmetic to
* minimize the number of calls to the random number generator without expensive
* calculations. The class is similar in operation to the
* {@code SecureRandom.nextInt(int)} method defined in Java SE.
*
* @author owlstead
*/
public final class JCSecureRandom {
private static final short SHORT_SIZE_BYTES = 2;
private static final short START = 0;
private final RandomData rnd;
private final byte[] buf;
/**
* Constructor which uses the given source of random bytes. A two byte
* buffer transient buffer is generated that is cleared on deselect.
*
* @param rnd
* the source of random bytes
*/
public JCSecureRandom(final RandomData rnd) {
this.rnd = rnd;
this.buf = JCSystem.makeTransientByteArray(SHORT_SIZE_BYTES,
JCSystem.CLEAR_ON_DESELECT);
}
/**
* Generates a single short with a random value in the range of 0
* (inclusive) to the given parameter n (exclusive).
*
* @param n
* the upper bound of the random value, must be positive
* (exclusive)
* @return the random value in the range [0..n-1]
*/
public short nextShort(final short n) {
final short sn = (short) (n - 1);
short bits, val;
do {
bits = next15();
val = (short) (bits % n);
} while ((short) (bits - val + sn) < 0);
return val;
}
/**
* Generates a single byte with a random value in the range of 0 (inclusive)
* to the given parameter n (exclusive).
*
* @param n
* the upper bound of the random value, must be positive
* (exclusive)
* @return the random value in the range [0..n-1]
*/
public byte nextByte(final byte n) {
if ((n & -n) == n) {
return (byte) ((n * next7()) >> 7);
}
final byte sn = (byte) (n - 1);
byte bits, val;
do {
bits = next7();
val = (byte) (bits % n);
} while ((byte) (bits - val + sn) < 0);
return val;
}
/**
* Generates 15 bits from two bytes by setting the highest bit to zero.
*
* @return the positive valued short containing 15 bits of random
*/
private short next15() {
this.rnd.generateData(this.buf, START, SHORT_SIZE_BYTES);
return (short) (Util.getShort(this.buf, START) & 0x7FFF);
}
/**
* Generates 7 bits from one byte by setting the highest bit to zero.
*
* @return the positive valued byte containing 7 bits of random
*/
private byte next7() {
this.rnd.generateData(this.buf, START, SHORT_SIZE_BYTES);
return (byte) (this.buf[START] & 0x7F);
}
}
请指出这是否回答您的问题,或者如果您需要更多的细节。 –
- 1. 两个数字之间的随机数字生成器 - MatLab
- 2. 在两个数字之间生成随机数
- 3. 在Objective-C中的两个数字之间生成随机数字
- 4. 在两个限制之间生成随机数字
- 5. Java在两个给定值之间生成随机数
- 6. 如何合计两个随机生成的数字? Java的
- 7. 随机数生成两个区间之间的双打[a,b]
- 8. 生成唯一的随机数字JAVA
- 9. 在两个整数值之间生成Swift中的随机值
- 10. 如何在Swift中生成0-5之间的随机数字?
- 11. 在两个值之间生成一个随机数
- 12. 随机生成数字?
- 13. 生成两个范围之间的随机整数
- 14. 使用随机数生成器生成5到25之间的数字
- 15. 之间的C#生成随机小数两位小数
- 16. 在数组中生成随机数字
- 17. 在Python中的数组中生成两个值之间的随机数
- 18. 在2个数字之间生成一个唯一的随机数
- 19. 生成0到10之间的随机数字
- 20. 我如何随机生成75-100%之间的数字?
- 21. Javascript生成0到75之间的随机数字
- 22. Matlab与Python之间随机生成数字的精度差异
- 23. 如何生成-1000到1000之间的随机数字?
- 24. 我想用一个随机数生成两个cout的随机数之一
- 25. 如何在Java中生成一个随机的9位数字?
- 26. 在[0,1)之间生成随机数
- 27. 两个数字之间的随机漂浮?
- 28. 如何在1-10之间生成随机数字?
- 29. 如何在Haskell中随机生成一个随机生成的数字列表
- 30. 创建在两个参数之间生成随机数的函数C
[你尝试过什么?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Walls
使用RandomData抽象类 –