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 Slobo89 · Jun 21, 2013 at 11:00 AM · randomnumber

Random gen script giving same numbers 3 times in a row

I am creating level on a run time. And i am using random generator for it. Problem is in that random gen is giving me 2 or 3 same nubmers in a row. If anyone know how to fix this script it will be helpfull, becouse i am stuck, and dont know what to do... I searched forums and triead many scripts but i didnt make them work. And also sorry for bad english :D

 #pragma strict
 import System.Collections.Generic;
 
 var numbers : List. < int > ;
 static var outputNumber : int; //this number is going to other script for spawning
 
 function Update () 
 {
         numbers = new List. < int > (6);
         for (var i = 0; i < 6; i++) 
             {
                numbers.Add(i);
                }
         
         var randomNumbers = new int[1];        
         for (i = 0; i < randomNumbers.Length; i++) 
             {
               var thisNumber = Random.Range(0, numbers.Count);
                randomNumbers[i] = numbers[thisNumber];
               numbers.RemoveAt(thisNumber);
             }
         outputNumber = thisNumber;        
             
 }
 
Comment
Add comment · Show 5
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 Graham-Dunnett ♦♦ · Jun 21, 2013 at 11:03 AM 0
Share

Sometimes when you toss a coin, you get heads come up a number of times in a row. That's how random numbers work. If you got the same number 700 times in a row, then I'd say that that was unexpected, but just three times? Sounds okay to me.

avatar image Slobo89 · Jun 21, 2013 at 12:38 PM 0
Share

I need to prevent that... Problem is if i get same number to many times, other thing wont work...

avatar image fjalla · Jun 21, 2013 at 12:45 PM 0
Share

It's easy actually. Just check if the output number is the same as the previous output number. If you need help I'll try to modify the script for you.

avatar image Slobo89 · Jun 21, 2013 at 12:58 PM 0
Share

I would be most greatfull if u modify script. I tried so many ways but i have no results.

avatar image fjalla · Jun 21, 2013 at 01:06 PM 0
Share

I'll try then :)

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Ben-Stoneman · Jun 21, 2013 at 12:46 PM

As Graham has said, 3 in a row is random.

However you can add little modifications to get the result you want. For example if a combat battle has a 1 in 2 chance of success, the player will be feeling as though the game is cheating if the result is LOSE 3 times in a row.

Storing the last number and checking it with the new number would be a way to avoid the same number being drawn out multiple times.

(this can all depend on what the purpose of the random number is for)

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 Slobo89 · Jun 21, 2013 at 01:00 PM 0
Share

Purpose of this random number is to spawn parts of map for sidescroller. If i get 3 same parts of map next part is not spawned, and my player just fall down.

avatar image
0

Answer by fjalla · Jun 21, 2013 at 01:39 PM

Something like this (maybe)?

 #pragma strict
 import System.Collections.Generic;
  
 var numbers : List. < int > ;
 var outputNumber : int; //this number is going to other script for spawning
  
 function Update () {
     
     numbers = new List. < int > (6);
     
     for (var i = 0; i < 6; i++) {
         numbers.Add(i);
     }
  
     var randomNumbers = new int[1];
     var previousNumber : float;
            
     for (i = 0; i < randomNumbers.Length; i++) {
            
            var randomNum = RandomNum (numbers.Count);
         
         while (randomNum == previousNumber) {
             randomNum = RandomNum (numbers.Count);
         }
          
         if (randomNum != previousNumber) {
             previousNumber = randomNum;
              var thisNumber = randomNum;
             randomNumbers[i] = numbers[thisNumber];
              numbers.RemoveAt(thisNumber);
                outputNumber = thisNumber;
        }
     }       
 
 }
 
 //Use a function for randomness, easier to modify
 
 function RandomNum (max : int) : int {
     var num = Random.Range(0, max);
     //Return a random number
     return num;
 }


But it sometimes seems to stop for a moment.

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
avatar image
0

Answer by AntiLunchBox · Jun 21, 2013 at 01:39 PM

What you are doing is a little unneccessary, but you can also ensure randomness by adding an extra factor. Have it so that you grab from different halfs of your random numbers each time. So it'll alternate randoms:

 var numbers : int[] = new int[] { 0,1,2,3,4,5};
 var firstHalf : bool = true;
 
 static var outputNumber : int; //this number is going to other script for spawning
  
 function Update () 
 {
        if(firstHalf)
        {
            outputNumber = Random.Range(0, numbers.Count/2);
        }
        else
        {
            outputNumber = Random.Range(numbers.Count/2, numbers.Count);
        }
        firstHalf = !firstHalf;
 }
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 AntiLunchBox · Jun 21, 2013 at 01:40 PM 0
Share

excuse my javascript, i'm a little rusty so there may be some c# isms in there--but you should be able to get the jist.

avatar image AntiLunchBox · Jun 21, 2013 at 01:45 PM 0
Share

It also depends on how often you are using outputNumber, if you're not using it every frame--then you should use a different approach. Where you don't have your random number generator run in Update. Have it run in a different function WHEN you need to grab the number.

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

18 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

Related Questions

Assigning random numbers to variables in the awake function 1 Answer

For Loop isn't working properly! 1 Answer

Edit - Code isolation using random ID numbers C# 1 Answer

Room generator spawn 1 unique room 1 Answer

Random Numbers and Array Assignments 3 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