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
1
Question by chanfort · Mar 14, 2015 at 11:36 PM · randomrandom.range

Rolling random numbers

Lets say I have integers between 1 and 10. First time I chose randomly integer with Random.Range(1,10). Let's say it returns 8. Now I want to get number again from 1 to 10, but I want that it would not return 8 now. So basically I want that each time number is rolled, I want that it won't be rolled again on next roll. How it would be easiest to implement this?

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
2
Best Answer

Answer by el-pepi · Mar 15, 2015 at 02:32 AM

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class RandomNumber : MonoBehaviour {
 
     List<int> numberList = new List<int>();
 
     void Start () {
         int numberAmmount = 10;
         while(numberAmmount > 0){
             numberList.Add(numberAmmount);
             numberAmmount--;
         }
     }
 
     int GetRandomNumber(){
         if(numberList.Count == 0)
             return -1; //there are no more numbers left
         int i = Random.Range(0,numberList.Count);
         int toReturn = numberList[i];
         numberList.RemoveAt(i);
         return toReturn;
     }
 }

Here you go, I didn't test this tho.

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 chanfort · Mar 15, 2015 at 10:44 PM 0
Share

Looks great, thanks a lot!

avatar image
1

Answer by Eluate · Mar 14, 2015 at 11:48 PM

Just save the last rolled int as a variable

 int oldInt = 0;
 while (rolling)
 {
     int rolledint = Random.Range(1,10);
     if (rolledint != oldInt)
     {
         Debug.log("Rolled: " + rolledint);
     }
 }

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 chanfort · Mar 15, 2015 at 01:38 AM 0
Share

it would overwrite oldInt at second roll, i.e. if first roll oldInt becomes 8, at second roll it becomes 6 then at third roll there is again possibility to roll 8 :(

avatar image
0

Answer by diego_prado · Nov 07, 2016 at 06:33 PM

Hi @chanfort

My solution was to make an array of the "valid numbers", and always ask a random for the position in that array. Check this code:

  private static List<int> Generate(List<int> initialNumbers)
  {
             List<int> generatedNumbers = new List<int>();
             Random rnd = new Random(DateTime.Now.Millisecond);
             int position, nextNumber;
             while (initialNumbers.Count > 0)
             {
                 position = rnd.Next(initialNumbers.Count());
                 nextNumber = initialNumbers.ElementAt(position);
                 generatedNumbers.Add(nextNumber);
                 initialNumbers.Remove(nextNumber);
             }
             return generatedNumbers;
 }

The function gets an array of the "valid numbers" and return and array "shuffled". This has been already used and tested with 1000 numbers.

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

Random.Range(..) not working 1 Answer

How do you get different random numbers for each object array? 1 Answer

Random.Range not working. 1 Answer

Using Vector3 in ViewportToWOrldPoint 1 Answer

How good is Random.Range() for Uniform Distribution? 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