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 /
avatar image
0
Question by Tea_Doogun · Aug 14, 2012 at 07:39 PM · randomrangenumbers

Disreguard random number after use...

Using Random.Range, i choose a number at random from 1-10.

But i only want each number to be used once, so if i roll a 3, then re-roll 3 i want it to either roll again, or take 3 out of the list of randomly chooseable (did i just make up a word?) numbers.

Are either of these things possible?

Thanks, Tom :)

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

3 Replies

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

Answer by mikebelotti · Aug 14, 2012 at 07:56 PM

Sure. You'll just need to make a random number "pool" of sorts. This can just be a List or something similar. Fill it with the numbers you want, then do a Random.Range from 0 to the length of the list. Get the value, remove that index from the list, and repeat until the list is empty.

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 Screenhog · Aug 14, 2012 at 09:41 PM 1
Share

You could also shuffle the array, and then pick the numbers, one by one. (A search for "shuffle array" will likely give you many different ways to do this.)

avatar image
1

Answer by Mander · Aug 14, 2012 at 08:13 PM

sure just store ur last number in a variable

lets say ur first roll is 3 then u store it in a variable

and compare.

 if (the new number == to the last number){
 re-roll again.
 }
Comment
Add comment · Show 12 · 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 IndieScapeGames · Aug 14, 2012 at 08:30 PM 1
Share

What I would do, +1.

avatar image Muuskii · Aug 14, 2012 at 08:32 PM 3
Share

Hopefully you don't have an unlucky player who gets stuck in an infinite loop! xD

avatar image Mander · Aug 14, 2012 at 09:04 PM 1
Share

ahahah that's not possible, unless only 1 value is used xD

avatar image mikebelotti · Aug 14, 2012 at 09:20 PM 1
Share

Provided you change the if to a while (to guarantee a different number is used), it is possible to get stuck in an infinite loop there. In practice, though, it probably won't happen. But, if the range of values is small, the code might get caught in that loop for quite a few cycles, causing a slight frame rate drop.

avatar image Mander · Aug 14, 2012 at 09:25 PM 1
Share

that is true. so it could be so ezly fixed. maybe u dont reroll if the number is the same just take the number and +1 or + another random. :)

Show more comments
avatar image
1

Answer by sman_47o · Aug 14, 2012 at 08:28 PM

the only on or two ways i can think of that are different than those already posted at the moment would be to either the best seems to be:

have an array of already rolled numbers that gets added to every roll and then if a rolled number is equal to any number in that array the code would roll again until it got a number not included in the array. when the array got filled it would be reset.

so for example (sorry this is off the top of my head):

 var rolledNumbers : int[]
 var rolledNumber : int;
 function Start(){
     rolledNumbers = new int[10];
 }
 function onRoll(){
     var tempNum : int = Random.Range(1,10);
     if( rolledNumbers[tempNum]=tempNum){
         return;
         }
         else{
             rolledNumber = tempNum;
             rolledNumbers[tempNum] = tempNum;
         }
     }
 }
 
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

14 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

Related Questions

Excluding values from a Random.range? 1 Answer

Random.Range statement question 1 Answer

Various of Random Numbers? 1 Answer

Picking one of two numbers. 1 Answer

Random Range with limits 2 Answers


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