我们被要求用 Java 构建一个随机字母库。我做了功课。我们现在被要求用 C 语言编写。我用 Java 中的 nanotime 做到了。我做了一些研究,但我在 C 中找不到纳米时间。
已经在 C 中尝试过:(它没有用)
Random SayiOlustur(){
Random this;
this = (Random) malloc(sizeof(struct RANDOM));
char randomly;
int i;
struct timeval before , after;
gettimeofday(&before , NULL);
for (i=1 ; i <= 100 ; i++){
printf("%d %d %d ",i, i*i, i*i*i);}
gettimeofday(&after , NULL);
printf("%d",Nano(before,after));
int random;
double test;
while(true){
test = Nano(before,after);
random = (int)test % (int)123.0;
if((random >= 65 && random <= 90) || (random >= 97 && random <= 122)){
randomly = (char) random;
break;
}}}
int Nano(struct timeval x,struct timeval y){
double x_ms , y_ms , diff;
x_ms = (double)x.tv_sec*1000000 + (double)x.tv_usec;
y_ms = (double)y.tv_sec*1000000 + (double)y.tv_usec;
diff = (double)y_ms - (double)x_ms;
return diff;
}
Java代码:(工作)
private long Now(){
long now = System.nanoTime();
return now;
}
public char Random(){
char rastgele;
while(true){
random = (int)((Now())%123);
// ASCI
if((random >= 65 && random <= 90) || (random >= 97 && random <= 122))
break;
}
rastgele = (char) random;
return rastgele;
}
public static void main(String[] args) {
RastgeleKarakter rastgele = new RastgeleKarakter();
System.out.println("Rastgele Karakter: " + rastgele.Random());
}
出去:
run:
Rastgele Karakter: m \\ Random
出去:
run
Rastgele Karakter: y \\ Random
慕少森
相关分类