- Home /
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;
}
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.
I need to prevent that... Problem is if i get same number to many times, other thing wont work...
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.
I would be most greatfull if u modify script. I tried so many ways but i have no results.
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)
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.
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.
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;
}
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.
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
![](https://koobas.hobune.stream/wayback/20220613104605im_/https://answers.unity.com/themes/thub/images/avi.jpg)