How to Arrange numbers Randomly

Hey, I am making a security code for a door and the code is working ok so far. The player has 10 seconds to punch in the correct code. If the code is punched in correct then the door opens. This all works but only because I know the secret code.

So, what I want to do is declare a variable of type int(var secretCode :int), and set its value to random.range(100,999) that way the security code is different each time you encounter this lock. The players only hint at the secret code is a onscreen GUIText that displays 3 scrambled numbers (GUIText). These numbers represent the actual code but are scrambled.

Example would be actual code = 321 and code hint = 213…

The player has to get the code correct within the 10 seconds or whatever number is set in the final version. As of right now I have the number reseting after it is correct.

Is there a way to do this using just Random.Range? My issue is how would I set the number to 321 and have the hint scrambled. Here is my code so far.

#pragma strict
//timer variables attach to game manager and drag GUI text into TimerText slot in inspector
var timer = 0.0;
var startTime = 0.0;
var maxTime = 20.0;
var timerText : GUIText;

//secret code variables
var secretCode : String;
var playerCode : String;
var newCode : int;

//place to enter playerCode
var text : String;
var done : boolean = false;
var codeHint : GUIText;

//score variables
var score = 0;
var scoreGUI : GUIText;



function Start () 
{
	newCode = Random.Range(100,999);
		secretCode = ""+newCode;
			codeHint.text = ""+secretCode;
}

function Update () 
{
	timer += Time.deltaTime;
		Timer ();
			
	HitDone();
			
			
}

function Timer ()
{
	if(timer > maxTime)
	{
		timer = startTime;
	}
}

function OnGUI()
{
	
	if(done)
	{
	text = GUI.TextField (new Rect(500,25,100,30), text, 3);
	GUI.Label (Rect (10, 10, 100, 20), "You Entered " + text );
		timerText.text = ""+timer;
			scoreGUI.text = ""+score;
			}
			else
			{
	GUI.Label (Rect (10, 10, 100, 20), "Enter Number");
	
	text = GUI.TextField (new Rect(500,25,100,30), text, 3);
	}
}

function HitDone()
{
	if(Input.GetKeyDown("space"))
	{
		done = true;
		CheckCode();
		
	}
}


function CheckCode()
{
	if(text == secretCode)
	{
		score+=100;
		
		var newCode2 = Random.Range(100,999);
		
		text = "";
		
		Reset();
		
	}
	else
	{
		score = score;
	}
}


function Reset()
{
newCode = Random.Range(100,999);
	secretCode = ""+newCode;
			codeHint.text = ""+secretCode;
}

I think I’d have two methods, one takes nothing and returns a random passcode, the other takes that passcode and returns a re-arranged hint. The re-arranging could be accomplished in many ways, so it comes down to preference and ease. Sounds like what you want is a randomized sequence of indicies which you’ll use for building a string from another string’s characters, which is just a bare-bones shuffle algorithm. There’s bound to be better info on this concept than I could write up on short notice, so I’d start looking using that keyword. Neat mini game, simple concept and execution. Kudos!

edit -
Hey, if it’s just going to be three or four characters at most, you could have a simple recursive check to ensure no duplicate indicies. This would do the job just as well.

have look at shuffle bags. basicallyyou put the indices (0,1,2) of the three characters in the number string in a list. then you choose a random element from that, add the char with the index at the string(builder) and remove the choosen element from the list. the repeat until no more values are in that list.
but you should check if the scrambeled value is different as chances are high you create the same value. so run this method again.

a simple algorithm for shuffling items is the “Fischer yates shuffle”. I’ve used it a couple of times when i’ve written card games.

//new random generator
private rnd = new Random()

//array that holds the numbers you want to shuffle
private List<int> yourArray = new List<int>();

public void addNumbers(int number1, int number2, int number3){
    yourArray.add(number1);
    yourArray.add(number2);
    yourArray.add(number3);
}

//fischer yates shuffle
public void shuffle(){

    int N = yourArray.length;
    for (int i = 0; i < N; i++) {
    
        int r = i + (int) (rnd.next(int min, int max) * (N - i));
        
        int swap = yourArray.get(r);
        yourArray.set(r, yourArray.get(i));
        yourArray.set(i, swap);
    }
}

I think this should work. Modified it a bit last time i used it was when i programmed a cardgame in java.

http://www.dotnetperls.com/fisher-yates-shuffle

I am having trouble grasping the concept in regards to how to apply it in my case. How would I pull the numbers generated by random.range for the secret code into an array. Also how would i split the numbers typed by the player in the text field so that i could check each number to see if what they typed matches the secret code if it is stored in an array. I was reading something about splitting the numbers using (“,”[0])

okey it’s not that hard of a problem.

you could do something simple.

When game starts generate your super secret code using a random and get 3 random numbers.
Store the 3 numbers in an array.

when you are going to create your “scrambled” code use either a shuffeling algorithm (like the fischer yates shuffle) this all depends on how complicated you want your code to be and if you want the functions to be dynamic.

If you want dynamic code then use a shuffeling algorithm, but store the “original code”.

“What do i mean by dynamic code?”

Well if you want to be able to change an int saying “i dont want only 3 numbers for my passkey i want 4, 5 or even 6” (if you’ve got different difficulties) then you have to code everything dynamically so that your functions are based on the input and not use fixed numbers and fixed number of if-statements.

okey, so we have a code and we have a scrambled code. We show the “scrambled” code to the player.

So then we come to the input.

How does he input the code? if the player is supposed to input the code all in one go like 346725375 then your actually dealing with ONE number and not 9 seperate numbers.

if the player is going to input one number at the time 1,4,6,2,7 then just store each number in an array and match the array to your secret code array and bob’s your uncle!

if too few numbers just cast an error, too many, cast an error.

Okey lets say you have your super secret code and you want to input it all in one go: 276574. Then my advice would be to take your super secret code array, loop out each number, convert them to strings and add them up: “896” + “789” + “745” = 896789745 and check that string against the input that player is punching in (converted to string ofc).