• 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 glaiveorb · Jul 24, 2012 at 06:53 PM · randomnumbergeneratorunique

Generate 8 unique random integers

Hey guys, im currently doing a random integers programme. Here's what i got so far

    FirstVal=Random.Range(0,numOfPeeps);  
  SecondVal=Random.Range(0,numOfPeeps); 
  ThirdVal=Random.Range(0,numOfPeeps); 
  FourthVal=Random.Range(0,numOfPeeps); 
  FifthVal=Random.Range(0,numOfPeeps);
  SixthVal=Random.Range(0,numOfPeeps);
  SeventhVal=Random.Range(0,numOfPeeps);
  EightVal=Random.Range(0,numOfPeeps);
  
  while(FirstVal==SecondVal)
  {
  SecondVal=Random.Range(0,numOfPeeps);
  }
  
  if(numOfPeeps>=3){
  while(ThirdVal==FirstVal||ThirdVal==SecondVal)
  {
  ThirdVal=Random.Range(0,numOfPeeps);
  }
  }
  
  if(numOfPeeps>=4){
  while(FourthVal==FirstVal||FourthVal==SecondVal||FourthVal==ThirdVal)
  {
  FourthVal=Random.Range(0,numOfPeeps);
  }
  }
  
  if(numOfPeeps>=5){
  while(FifthVal==FirstVal||FifthVal==SecondVal||FifthVal==ThirdVal||FifthVal==FourthVal)
  {
  FifthVal=Random.Range(0,numOfPeeps);
  }
  }
  
  if(numOfPeeps>=6){
  while(SixthVal==FirstVal||SixthVal==SecondVal||SixthVal==ThirdVal||SixthVal==FourthVal||SixthVal==FifthVal)
  {
  SixthVal=Random.Range(0,numOfPeeps);
  }
  }
  
  if(numOfPeeps>=7){
  while(SeventhVal==FirstVal||SeventhVal==SecondVal||SeventhVal==ThirdVal||SeventhVal==FourthVal||SeventhVal==FifthVal||SeventhVal==SixthVal)
  {
  SeventhVal=Random.Range(0,numOfPeeps);
  }
  }
  
  if(numOfPeeps>=8){
  while(EightVal==FirstVal||EightVal==SecondVal||EightVal==ThirdVal||EightVal==FourthVal||EightVal==FifthVal||EightVal==SixthVal||EightVal==SeventhVal)
  {
  EightVal=Random.Range(0,numOfPeeps);
  }
  }
  
Comment
Add comment · Show 1
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 whydoidoit · Jul 24, 2012 at 06:55 PM 0
Share

What language are you using?

2 Replies

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

Answer by whydoidoit · Jul 24, 2012 at 06:58 PM

    using System.Linq;
    using System.Collections.Generic;

    var availableEntries = Enumerable.Range(1, numOfPeeps).ToList();

    firstVal = availableEntries[Random.Range(0, availableEntries.Count)];
    availableEntries.Remove(firstVal);

    secondVal = availableEntries[Random.Range(0, availableEntries.Count)];
    availableEntries.Remove(secondVal);

etcetera...

Comment
Add comment · Show 2 · 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 whydoidoit · Jul 24, 2012 at 07:03 PM 1
Share

Or make the list in a random order like this:

    var availableEntries = Enumerable.Range(1, numOfPeeps).OrderBy(b=>Random.value).ToList();
    firstVal = availableEntries[0];
    secondVal = availableEntries[1];
avatar image DaveA · Jul 24, 2012 at 08:27 PM 0
Share

You want a 'deal' function, that's essentially what this OrderBy thing does. Create list of random numbers, sort them, keeping track of their indices. Resulting sorted list has 'random' indices, each, of course, unique.

avatar image
0

Answer by glaiveorb · Jul 25, 2012 at 06:14 AM

Hey thanks for the feedback guys! Turns out it was an error with the numOfPeeps as i was not setting it properly!! Thanks btw!!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Random Number Generator 4 Answers

RNG output to a variable + instantiating a prefab based on said variable? 1 Answer

Random texture changer (problem with array) 2 Answers

Randomly generated number 0 Answers

Random.Range(..) 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