Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

random numbers

I'm looking for a couple of ways of getting random numbers

First way
Picking random numbers from the inventory

Example:
Say player has items with IDs: 1, 5, 14, 16, 19, 22, 27, 31
Pick 5 random ones: 5, 16, 19, 22, 27, 31

Store each number into variable:
num1 -> 62
num2 -> 63
num3 -> 64
num4 -> 65
num5 -> 66

Second way
Picking random numbers from list

Example:
List has these ids: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25
Pick 5 random ones: 1, 7, 9, 17, 25

Store each number into variable

num1 -> 67
num2 -> 68
num3 -> 69
num4 -> 70
num5 -> 71

How would i go about doing this?
 
Code:
# Getting items list

array = []

for i in 1...$data_items.size

  array << i if $game_party.item_number(i) > 0

end

 

# Getting random value and removing it from Array

index = rand(array.size)

id = array[index]

array.delete_at(index)

 

# Setting to variable

$game_variables[n] = id

Hope that helps.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top