Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by alone1992 · May 29, 2015 at 08:11 AM · javascriptrandomdifferentnumberstwo

Two different random numbers

Hello every one

I want to have 2 different random numbers in other word I was wrote this code for my game that makes different places and never shouldn't chosen prefab be same as chosen position. I don't how to do it?

I think one of them could be random and other one be serial and then that one random shouldn't be repeated and also the chosen prefab shouldn't be same with chosen position.

 function Replace (){
     
     var PrefabsList : List.<GameObject> = new List.<GameObject>(prefabs);
     var PositionsList : List.<Vector3> = new List.<Vector3>(positions);
     
     while(PrefabsList.Count > 0){
         
         var ChosenPosition : int = Random.Range(0 , PositionsList.Count);
         var ChosenPrefab : int = Random.Range(0 , PrefabsList.Count);
         
     
             PrefabsList[ChosenPrefab].transform.position = PositionsList[ChosenPosition];
             PrefabsList.RemoveAt(ChosenPrefab);
             PositionsList.RemoveAt(ChosenPosition);
         
         
     }
     
     GameObject.Instantiate( chief_Prefab , twelve_InGame.transform.position , Quaternion.identity);
     Materials.CheckMat = true;
 }


Please answer code with Unity JS

thanks in advance

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by InsertFunnyUsernameHere · May 29, 2015 at 08:20 AM

So basicaly you want ChosenPosition to be different than ChosenPrefab? If so try this:

 While(ChosenPosition==ChosenPrefab)
 {
 ChosenPrefab=Random.Range(0,PrefabList.Count);
 }
Comment
Add comment · Show 4 · 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 alone1992 · May 29, 2015 at 08:32 AM 0
Share

thanks , but if you look at that code it removes the chosen ones from array list and that will hang on because both of them will be same number at the last one and it's 1 or 0 -_- I think should add a simple if(list.count > 2) and then your code with while and ...

avatar image alone1992 · May 29, 2015 at 08:45 AM 0
Share

unity not responds when I added it.

avatar image InsertFunnyUsernameHere · May 29, 2015 at 08:57 AM 1
Share

Okay then, modify this slightly

  While(ChosenPosition==ChosenPrefab&&PrefabsList.Count>1&&PositionsList.Count>1)
  {
  ChosenPrefab=Random.Range(0,PrefabList.Count);
  }

avatar image alone1992 · May 29, 2015 at 02:09 PM 0
Share

thanks I do that myself ^_^

avatar image
0

Answer by yebai · Nov 29, 2017 at 03:41 AM

A better way without using while() or other loops

 var ChosenPosition : int = Random.Range(0 , PositionsList.Count);
 var ChosenPrefab : int = (ChosenPosition + Random.Range(1 , PrefabsList.Count )) % PositionsList.Count;
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

21 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

Related Questions

How do Ensure that a series of int's are never the same? 1 Answer

How to create random movement in 2D 2 Answers

Random.seed on different hardware? 1 Answer

Disreguard random number after use... 3 Answers

How to use random numbers? 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