1

我想在 MIPS 中生成随机词。我知道如何生成随机数,我只想要一个词库中的随机词。我已经尝试过了,但我不知道如何打印它们。

.data
### WORD BANK ###
a0:     .asciiz "computer"
b1:     .asciiz "processor"
c2:     .asciiz "motherboard"
d3:     .asciiz "graphics"
e4:     .asciiz "network"
f5:     .asciiz "ethernet"
g6:     .asciiz "memory"
h7:     .asciiz "microsoft"
i8:     .asciiz "linux"
j9:     .asciiz "transistor"
k10:    .asciiz "antidisestablishmentarianism"
l11:    .asciiz "protocol"
m12:    .asciiz "instruction"
word:   .word   a0,b1,c2,d3,e4,f5,g6,h7,i8,j9,k10,l11,m12

.text
la $To,word

如何从给定列表中选择一个随机单词?

4

2 回答 2

2

如果您有一个在 int 范围内随机生成的数字,请使用除以字库大小(在本例中为 13)n的余数来查找您的数字的索引。n如果您有 RNG 的上限,请将其设置为字库大小。然后只需使用内存中的索引加载字符串。

于 2014-10-16T22:59:10.073 回答
-1

您可以创建一个数组,然后在一个生成随机数的对象内部使用一个 forEach 循环,您可以显示与随机生成的数字相对应的数组的单词。

这是 JAVA 代码,但我希望它会有所帮助

import java.util.Random;

//main class
public class Test1 
{

public static void main(String[] args) 
{       
    //Array of names
    String[] wordBank = {"luca", "serena", "giuseppe", "nicole", "eleonora", "elena", "matteo"};

    //random generation of names
    for(int i=1; i<10; ++i)
    {
        Random dice = new Random();
        int dice2 = dice.nextInt(6);
        System.out.println(wordBank[dice2]);    
    }

}

}

于 2014-10-18T19:23:59.907 回答