PlayHardGoPro
PlayHardGoPro

Reputation: 2933

Generate random number on MIPS

I found this possible solution but it gives me something like:

598756802  
373297426  
-2011171535   

It's like its printing the addres rather the number value. How to fix it?

Also I need to set a range to the generator. Like generate a random number between 0 to 99.
How could I do that ?
Obs: Already tried with syscall 42 code but I got an error.
(OFF: At least tell the reason for the downvote, I dont have a crystal ball).

code:

.text
    li $v0, 41          # Service 41, random int    
    syscall            # Generate random int (returns in $a0)
    li $v0, 1          # Service 1, print int
    syscall            # Print previously generated random int  

update And this always give me the same number, 1000.

.text
    li $a1, 2501
    li $v0, 42   #random
    add $a0, $a0, 1000
    li $v0, 1
    syscall

Upvotes: 0

Views: 29703

Answers (3)

Anton
Anton

Reputation: 11

All you need to generate random number is change upper bound and you will get it in register $s1. The following code will generate random number in range 0..3

addi $v0, $zero, 30        # Syscall 30: System Time syscall
syscall                  # $a0 will contain the 32 LS bits of the system time
add $t0, $zero, $a0     # Save $a0 value in $t0 

addi $v0, $zero, 40        # Syscall 40: Random seed
add $a0, $zero, $zero   # Set RNG ID to 0
add $a1, $zero, $t0     # Set Random seed to
syscall

addi $v0, $zero, 42        # Syscall 42: Random int range
add $a0, $zero, $zero   # Set RNG ID to 0
addi $a1, $zero, 4     # Set upper bound to 4 (exclusive)
syscall                  # Generate a random number and put it in $a0
add $s1, $zero, $a0     # Copy the random number to $s1

Upvotes: 1

Manoel Stilpen
Manoel Stilpen

Reputation: 1559

Your last code block was not calling the RNG syscall, only the print syscall.

It's simple, you just have to do the following

li $v0, 42  # 42 is system call code to generate random int
li $a1, 100 # $a1 is where you set the upper bound
syscall     # your generated number will be at $a0

li $v0, 1   # 1 is the system call code to show an int number
syscall     # as I said your generated number is at $a0, so it will be printed

If you want to store your generated numbers in an array, this is one way to do it:

move $s2, $s0      # copy the initial pointer to save array

li $v0, 42            # system call to generate random int
la $a1, 100       # where you set the upper bound
syscall              # your generated number will be in $a0

sb $a0, 0($s2)     # put the generated number at the position pointed by $s2

addi $s2, $s2, 1       # increment by one the array pointer

Since we only want a range of 0-100, we can store each number in a single byte instead of using sw and incrementing the pointer by 4.

Upvotes: 2

PlayHardGoPro
PlayHardGoPro

Reputation: 2933

I could make it work with the following code:

.text
    li $a1, 100  #Here you set $a1 to the max bound.
    li $v0, 42  #generates the random number.
    syscall
    #add $a0, $a0, 100  #Here you add the lowest bound
    li $v0, 1   #1 print integer
    syscall

Upvotes: 3

Related Questions