Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by AwesomeFaceHD · Apr 04, 2014 at 04:20 PM · c#listrandominventoryenum

Add random amount of random items from one list to another?

I have a list that contains every item in my game and a list list for chest contents. I want to randomly choose some items from the main items list to put in the chest list, but based on a rarity enum in the item class. Some ideas maybe?

Sorry I don't have any code, I'm at school right now, my stuff is at home. I don't think it is that necessary in this case anyways.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image roojerry · Apr 04, 2014 at 04:44 PM 0
Share

Look into weighted random numbers

avatar image borro56 · Apr 04, 2014 at 04:54 PM 0
Share

I dont undestand what you mean when you say based on an enum, the enum deter$$anonymous$$es the chance of the object to be added or you just want to add objects of certain type deter$$anonymous$$ed by that enum?

avatar image koray1396 · Apr 04, 2014 at 04:59 PM 0
Share

a simple way is, all items would have multipliers, very rares would have one, some 2, some 50 maybe, as if they are listed x times. you can find the total number, and generate a random number out of it. then you can project this number to the list item number. simply basic version of weighted random numbers.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Apr 04, 2014 at 05:58 PM

The "hard" part of your question is handling the rarity. I can think of two approaches. Both solutions involve defining a value for rarity rather than an enum. In the first one you need an integer value, and I'd do it in terms of abundance. So say your rarest item is given a value of 2. All other items are relative to the rarest. So if next rarest is twice abundant, it would get a value of 4. And if you had one that was 3 1/2 as abundant, it would get a value of 7.

Once defined, you construct the 'store' array. You populate it based on the abundance value. So you for the item with a value of 7, it will have 7 entries in the list. You populate the chest by simply selecting one from the length of the list.

The second approach is similar to the first in that you assign abundance values, but they don't have to be integers. So say the rarest has a value of 1.0, the next rarest might have a value of 1.5 for example. To get a random entry, you first sum all the abundance values. Then you generate a random value based on all the abuncance:

 val = Random.Range(0.0,totalAbuncance);

Then you walk your array adding up the abundance values as you go. When the sum is greater than your 'val', that is the entry that goes in the chest.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Xavier78 · Apr 04, 2014 at 05:42 PM

well i can push you in the right direction.

first I would make the rarity of the items so

var rarity = #;

and make it into a static var. Then in your chest script make it call your array of items. the chest should have a built in array of GameObjects var ItemsInChest = GameObject[size];

var size = 0; // make that how many items you want in the chest or/also

var min = 10; var max = 15;

function start() { size = Random.Range(min, max) }

// so it chooses random amount of items

now this is just a basic over vew, if you have the array already you can just make it static and then do

var ItemsToGet = 0;

function start() { // make how ever many randum itomes you want to be in chest // get those items from your items array //add those items to this chest array //set amount for those items }

now with out knowing the type of array you are using i have no more i really can say. hope it helped if not i will delete it to save space.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a camera list 1 Answer

Using enums to design an inventory system? 1 Answer

How can I remove an item ffrom a list. 1 Answer

Count object list duplicates 1 Answer

my random task generator not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges