pulling random numbers from an array or hash

vindaca

CEO of Tales of the Valiant
Veteran
Joined
Dec 11, 2012
Messages
534
Reaction score
75
First Language
English
Primarily Uses
Can rand pull numbers from an array or hash full of set numbers to choose from. Like current party member's id's or whatever.
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
maybe do something like

array[rand(array.length)]

if you need numbers from within the array...

then if you only need to randm from the indices

rand(array.length)
 
Last edited by a moderator:

vindaca

CEO of Tales of the Valiant
Veteran
Joined
Dec 11, 2012
Messages
534
Reaction score
75
First Language
English
Primarily Uses
Thank you I'm a little closer, it kinda worked.
Is there a way I can set up an unless with that rand to stop it from doing repeats?
 
This is what I currently have
I know there are still bugs I just want to set it up to do what I want first.
like right now it also works from the main menu and when you exit and return there are new skill.
 
Right now it is set up to pick 7 random skills out of all that actor skills. This is what I want but with out the repeats.
 

 #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>##                                                                              ##                           V's Random Battle Skills                           ##                                 Version  0.0                                 ##                                                                              ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##                                Written By:  V                                ##                          Last Edited: August 30, 2013                        ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##                                                                              ##<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>#  #==============================================================================##------------------------------------------------------------------------------## ** Disclaimer                                                                ##------------------------------------------------------------------------------##                                                                              ## This script was intended for Non-commercial use only, if you wish to use     ## this script in a commercial game please PM me at which ever site you found   ## this script. Either way please give me credit in your game script as the     ## writer of this script, and send me a PM abuot the release of the game/demo.  ##                                                                              ##------------------------------------------------------------------------------## ** How To Use                                                                ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##                                                                              ## * This script is pretty much plug-and-play.                                  ##                                                                              ##------------------------------------------------------------------------------## ** Description                                                               ##------------------------------------------------------------------------------##                                                                              ##  v0.0                                                                        ## ~=~=~=~                                                                      ##  * Still being written                                                       ##------------------------------------------------------------------------------##==============================================================================##<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>##                                                                              ##        DO NOT EDIT PASSED THIS POINT UNLESS YOU KNOW WHAT YOUR DOING.        ##                                                                              ##<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>#                    class Window_SkillList < Window_Selectable  def make_item_list    i = 0    td = []    @data = @actor ? @actor.skills.select {|skill| include?(skill) } : []    until i == 7      td = @data[rand(@data.size - 1)]      i += 1    end    @data.clear    @data = td  endend
 
maybe something like this?
I tried this and it didn't work. I that is because it is creating a new random number every time rand is called. I think.Its not prints out all of the names onto the screen though in the menu
 

Code:
class Window_SkillList < Window_Selectable  def make_item_list    i = 0    td = []    @data = @actor ? @actor.skills.select {|skill| include?(skill) } : []    until i == 7      td[i] = @data[rand(@data.size - 1)] unless td.include?(@data[rand(@data.size - 1)])      i += 1    end    @data.clear    @data = td  endend
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
I would copy the skills into another array (using .clone), and each time I chose one, I'd remove it from the array so it can't be chosen again.


Otherwise, if you were going to keep a list of the ones selected and keep going until you selected a new one, you could get yourself into an endless loop. Also you need to take into account the possibility that there aren't enough elements to fill your requirement for that many.
 

vindaca

CEO of Tales of the Valiant
Veteran
Joined
Dec 11, 2012
Messages
534
Reaction score
75
First Language
English
Primarily Uses
Thank you Shaz.

How do I access the clone or do I just call it @data.clone?
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
No, you create a new variable.


temp = @data.clone


then use temp instead of @data.
 

vindaca

CEO of Tales of the Valiant
Veteran
Joined
Dec 11, 2012
Messages
534
Reaction score
75
First Language
English
Primarily Uses
Isn't that the same as just doing

temp = @data
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
But this way, you can remove things from the clone without removing it from the original array...

coz if you do it like

temp = @data

modifying temp will also modify @data

but if you do it by

temp = @data.clone

modifying temp won't affect @data

Basically, if you set a variable equal to the array, it gives the reference to the array itself... but if you use clone, it creates first a new array, populates with the same data as that of the original array, then passes the reference to this new array...

That's how it is right, Shaz?
 
Last edited by a moderator:

vindaca

CEO of Tales of the Valiant
Veteran
Joined
Dec 11, 2012
Messages
534
Reaction score
75
First Language
English
Primarily Uses
temp = @data <----this is a clone right?

temp.remove

 

If I do this I'm not removing from @data I'm removing from temp.
 
Last edited by a moderator:

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
read above... I'm not sure though, but I think that's how it is since Shaz told you to use .clone

EDIT: oh I see... didn't look at the code posted... lol... you might be right... well, you can always try it first... see if it works without problems...

btw, the problem with your code is that if the condition is false, you still increment by 1... so if for example I roll a 1 for seven times it will still finish the loop, and I'll only have 1 skill...

You should only increment the iterator "i" if the obtained value fits the condition... though as Shaz said, you might end up with an infinite loop at times...

and the condition is also not good since you used rand twice... so you get two random numbers... if you want to do it that way, you should save the rand value into a variable first, then use that variable instead of using rand twice...

So using the removal method will be the best, though I just wanted to tell you what's wrong with that code... :)
 
Last edited by a moderator:

vindaca

CEO of Tales of the Valiant
Veteran
Joined
Dec 11, 2012
Messages
534
Reaction score
75
First Language
English
Primarily Uses
@Shaz, I did this to test the vars in the new code and something threw me off 

It is printing before and after 3 times each and it reduces the number to 5 out of 10 starting skills.

class Window_SkillList < Window_Selectable  def make_item_list    td = []    @data = @actor ? @actor.skills.select {|skill| include?(skill) } : []    td = @data.clone    (td.size - 1).times { |i|      rn = rand(td.size -  1),      td.delete(rn) unless td.size <= 7    }    p "Before"#~     p @data    @data.clear    @data = td    p "After"#~     p @data  endend@Adiktuzmiko that is not true, if you want to alter something you have to alter the original array, that was not my question. The way you set up text is...

default_text = Font.default_name

Font.default_name = whatever font

then your method or text

Font.default_name = default_text

Font.default_name is not changed until I change it.

The same would apply to this.

I tried it both ways and got the same result with @data with and without the clone.

I just wanted to know if clone did something different. I knew that temp = @data is just a copy. I don't know if their are added benifits to using clone. I have written several scripts and never saw a need to use .clone. Thank you for your help though. You did help me with the random numbers I didnt't think to set it up like that in the arrays elements, and you brought up a good point about the increments.
 
Last edited by a moderator:

Neon Black

The Classy Prostitute
Veteran
Joined
Mar 17, 2012
Messages
1,149
Reaction score
374
First Language
Sarcasm
Primarily Uses
If all you want to do is pull 7 random skills, you could just shuffle the array and return the first 7 elements, couldn't you?  Like so:

Code:
class Window_SkillList < Window_Selectable  def make_item_list    temp = @actor ? @actor.skills.select {|skill| include?(skill) } : []    @data = temp.shuffle[0, 7]  endend
 

vindaca

CEO of Tales of the Valiant
Veteran
Joined
Dec 11, 2012
Messages
534
Reaction score
75
First Language
English
Primarily Uses
No that just shuffles the order of the first seven.

However with that I can do this and it works. Thank you Neon Black, now I just have a few more adjustment then I can release it publicly. Thank you all for your help.

Code:
class Window_SkillList < Window_Selectable  def make_item_list    rand_skills = 6    temp = @actor ? @actor.skills.select {|skill| include?(skill) } : []    @data = temp.shuffle[0, (temp.size - 1)]    (@data.size - rand_skills).times { |i|    @data.delete(i) unless @data.size <= rand_skills }  endend 
 

Neon Black

The Classy Prostitute
Veteran
Joined
Mar 17, 2012
Messages
1,149
Reaction score
374
First Language
Sarcasm
Primarily Uses
No that just shuffles the order of the first seven.
You sure about that?  I just tested a similar scenario and it worked as I expected.  I have brackets after the shuffle because it's grabbing the first 7 numbers of the shuffled array.  "temp[0, 7].shuffle" would shuffle and return the first 7 elements.  "temp.shuffle[0, 7]" is pretty much similar to doing "temp.shuffle!" followed by "temp[0, 7]" but it saves you a line.

EDIT:  Pretty much I'm trying to help save any need to delete elements from the result array reducing overhead or prevent potentially bug ups with the script from nil values.
 
Last edited by a moderator:

vindaca

CEO of Tales of the Valiant
Veteran
Joined
Dec 11, 2012
Messages
534
Reaction score
75
First Language
English
Primarily Uses
I copied it exactly as you wrote it, and when I tried it in the script, and it is, it returns 6 special skills and 4 magic, that is all of the skill for that actor. That's why I added in Shaz' advice and just removed from the array until 7 remained, and since shuffle randomized it, it worked out perfectly. you can test it if you want. just add as many skills as you want and test it in the menu. 

I am not sure exactly how shuffle works I tried to find it in the help and it's not there, that's another one that I've never had to use.
 
Last edited by a moderator:

vindaca

CEO of Tales of the Valiant
Veteran
Joined
Dec 11, 2012
Messages
534
Reaction score
75
First Language
English
Primarily Uses
Now I just have to set it to the battle only and then stop it from changing every time you enter and exit the menus (I can do both of those things)
 

Neon Black

The Classy Prostitute
Veteran
Joined
Mar 17, 2012
Messages
1,149
Reaction score
374
First Language
Sarcasm
Primarily Uses
I copied it exactly as you wrote it, and when I tried it in the script, and it is, it returns 6 special skills and 4 magic, that is all of the skill for that actor. That's why I added in Shaz' advice and just removed from the array until 7 remained, and since shuffle randomized it, it worked out perfectly. you can test it if you want. just add as many skills as you want and test it in the menu. 
I just did and it worked exactly as I anticipated.  Odd.  Not entirely sure why it isn't working in your case.  Anyway, as long as you get it to work that's all that really matters.
 
Last edited by a moderator:

vindaca

CEO of Tales of the Valiant
Veteran
Joined
Dec 11, 2012
Messages
534
Reaction score
75
First Language
English
Primarily Uses
I am so sorry Neon Black I just tested it again like this to make sure it was returning the correct amount and it did. Not sure what I did the first time. Thanks and again I'm sorry.

class Window_SkillList < Window_Selectable
  def make_item_list
    temp = @actor.skills
    @data = temp.shuffle[0, 7]
  end
end
 for some reason when they are split up by types it was returning a set amount for each type. The way it is set up now it just returns all of the skills no matter the type and yes you were right it just shuffles all of them and returns the first seven.
 
Last edited by a moderator:

Napoleon

Veteran
Veteran
Joined
Dec 29, 2012
Messages
869
Reaction score
97
First Language
Dutch
Primarily Uses
I just wanted to know if clone did something different. I knew that temp = @data is just a copy. I don't know if their are added benifits to using clone. I have written several scripts and never saw a need to use .clone. Thank you for your help though. You did help me with the random numbers I didnt't think to set it up like that in the arrays elements, and you brought up a good point about the increments.
I'm sorry for ignoring the original question but just to answer this part.

Arrays and Hashes are "copied by reference" in Ruby by default. So:

a = [1,2,3]p a # [1,2,3]b = ab.shiftp a # [2,3]p b # [2,3]BUT

a = [1,2,3]p a # [1,2,3]b = a.cloneb.shift # remove first elementp a # [1,2,3] # <<<< now a is untouched!p b # [2,3]In languages like C++ this would be easier to see. In Ruby they somewhat 'suppress' the need to use pointers and whatever but in fact you are still using a lot of "pass by reference" (or 'pointers').

Why you need clone? Because you might want to perform a difficult search operation or bitmap operation or whatever on "a". Your algorithm might remove elements from "a" to find the last remaining element or use all sorts of bitmap functions on it. But you really don't want to alter "a" itself. In such cases you would need clone. Remember that "a" could also be an image or some sort.

Clone however does not clone anything that your variable itself may reference (aka just a shallow copy).
 
Last edited by a moderator:

vindaca

CEO of Tales of the Valiant
Veteran
Joined
Dec 11, 2012
Messages
534
Reaction score
75
First Language
English
Primarily Uses
Clone however does not clone anything that your variable itself may reference (aka just a shallow copy).
Where as b = a does right?
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

People3_5 and People3_8 added!

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.

Forum statistics

Threads
105,868
Messages
1,017,083
Members
137,583
Latest member
write2dgray
Top