• 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 /
avatar image
0
Question by schwertfisch · Feb 13, 2011 at 04:05 PM · builtin arrayrandom-range

control over the possibility of a Random.Range function from an array's elements

(I'm really sorry if the title is a mess, I can't put it simply in a few words)

I have a built-in Array, "myArray". Right now, I use this:

randomArrayElement = Random.Range (0,myArray.length);

to pick a random element from myArray. What I want is to increase the possibility of certain elements to be picked by this function.

Till now I've been doing it by inserting them more than once from within the editor (so the array has one instance of each element and say 5 instances of a certain element that I want to have randomly picked more frequently).

Is there a code-based way to do this? Thanks :)

Comment
Add comment · Show 4
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 Peter G · Feb 13, 2011 at 06:10 PM 0
Share

Do you need a common curve such as bell curve or is it more arbitrary?

avatar image schwertfisch · Feb 13, 2011 at 06:20 PM 0
Share

No not a bell/gaussian curve. It will have to be totally arbitrary, eg I have 10 elements in myArray and I want e.g. the 1st to be 50% possible to be "randomly" picked and all the others will equally share the rest 50%

avatar image Peter G · Feb 15, 2011 at 12:55 AM 1
Share

Not exactly what you want, actually very close to what you are already doing: http://processing.org/discourse/yabb2/YaBB.pl?num=1260461297

avatar image schwertfisch · Feb 15, 2011 at 07:15 PM 0
Share

Thanks a lot Peter, this was really helpful, could you put this in an answer so that I can accept it?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Meltdown · Feb 13, 2011 at 08:08 PM

Hi schwertfish,

You can do something like this in C#

Basically create a for loop, that runs 5 times, in each iteration, if the first element is returned, you break out of the for loop, if not the loop will try another 4 times to try and return the first element, else it will return another element. You can adjust the values of course to determine how often, and which element you would prefer to get out.

for(int i = 0; i < 5; i++) { randomArrayElement = Random.Range(0,5);

// If the first element (which is the preferred element is chosen), exit the loop if(randomArrayElement == 0) break; }

Or if you want multiple elements to be favoured, such as the 2nd and 4th element..

for(int i = 0; i < 5; i++) { randomArrayElement = Random.Range(0,5);

// If the preferred 2nd or 4th elements are chosen, exit the loop if(randomArrayElement == 1 || randomArrayElement == 3) break; }

Comment
Add comment · Show 1 · 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 schwertfisch · Feb 14, 2011 at 09:29 AM 0
Share

Thanks $$anonymous$$eltdown, but I wasn't looking for something like this. I am using a built-in Array, which I can redefine anytime I want (I could for example redefine it through code and make it contain only the desired elements - 1 & 3 as you say), but I was wondering if there is a more "mathematical" way of making the Random.Range a bit biased.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Is there a way to define arrays and objects literally (i.e. assign all values in one go, inline)? 4 Answers

custom function for manipulating builtin array 1 Answer

Clearing Dynamically GameObject Array 1 Answer

Create an array of gameobjects in trigger? 1 Answer

built in array in class 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges