Hi all, I am having trouble trying to generate an array of unique random numbers (ie no number is the same within the array). I tried on my own for a while until I resorted to grabbing some javasrcipt code from the net and modifying to work in unity. When I run this however my nums array prints out as ,
Any ideas?
var random_number :int;
function create_unique_random_array(num_elements,min,max) {
var temp = new Array();
var nums = new Array();
random_number =Random.Range(min,max);
for (var element=0; element<num_elements; element++) {
//IMPORTANT: DON'T FORGET THE SEMI-COLON AT THE END
while((temp==number_found(random_number,nums))==-1);
nums[element] = temp;
}
return (nums);
}
function number_found (random_number,number_array) {
for (var element=0; element<number_array.length; element++) {
if (random_number==number_array[element]) {
return (-1);
}
}
return (random_number);
}
I have no idea what this line of code is supposed to do, but I can tell you it’s completely equivalent to:
while (false);
First it compares a number coming from “number_found” to an array. Since an array is not a number, it returns false. Then it compares false to -1, which, again results in false.
Frankly, I’m surprised this even compiles at all :P. I’d try to step away from writing everything into a single incomprehensible statement and write out the algorithm into its individual steps instead.
Cheers! Yeah it doesn’t look like var temp needs to be an array. I’ve altered the code slightly and now I’m getting Nums: 0,0,0 (hmm still not good)
To be honest I personally don’t understand the syntax of the while loop in this one and can’t find any decent documentation to explain.
Surely there is a simple well documented way to produce a unique list of random numbers, this kind of thing is so useful in just about all games isnt it?
Anyway, thanks again…my new (useless again …but slightly less so than before)code below.
function create_unique_random_array(num_elements,min,max) {
var temp :int ;
var nums = new Array();
random_number =Random.Range(5,70);
for (var element=0; element<num_elements; element++) {
//IMPORTANT: DON'T FORGET THE SEMI-COLON AT THE END
while((temp==number_found(random_number,nums))==-1);
nums[element] = temp;
}
return (nums);
}
function number_found (random_number,number_array) {
for (var element=0; element<number_array.length; element++) {
if (random_number==number_array[element]) {
return (-1);
}
}
print("Random number is :"+random_number);
return (random_number);
}
Well, the first ‘==’ in the while is probably supposed to be a ‘=’, but I don’t know if that will make it work.
The source code you’re trying to port is awful, as it manages to turn what is supposed to be a simple task into something you need at least 5 minutes to understand. I’d look for a different implementation.
Try something like this (untested):
var list : ArrayList = new ArrayList();
while (list.Length < desiredArrayLength)
{
var number = Random.Range(min, max);
if (!list.Contains(number))
list.Add(number);
}
Hmm, well that didnt work. Seems that there is no ‘Contains’ for the Array class and if its a new object (ArrayList) then ArrayList.length wont work.
I tried something similar with a hash table but as I have absolutley no idea about hash tables I am pissing into the wind somewhat. Howerever this code doesnt work either…hmmm…
function create_unique_random_array(desiredArrayLength,min,max) {
var list :Hashtable = new Hashtable();
var count:int=0;
while (count< desiredArrayLength)
{
var number = Random.Range(min, max);
if (!list.ContainsValue(number))
{
list.Add(count,number);
count++;
}
}
}
…however he following error results…
Assets/Standard Assets/Scripts/MyScripts/QuizBehaviourScript.js(220,64): BCE0022: Cannot convert ‘void’ to ‘Object’.
I’d get familiar with the Mono docs if you’re doing anything outside the Unity API. Sure, there’s no length property to ArrayLists, but there is a Capacity and a Count property (since there’s a difference between capacity and the number of elements present in an ArrayList). Generally, Googling “Mono <whatever class you’re looking for> documentation” will yield the desired result. So the revised code might look like
var list : ArrayList = new ArrayList(desiredArrayLength);
while (list.Count < list.Capacity)
{
var number = Random.Range(min, max);
if (!list.Contains(number))
list.Add(number);
}
Oops, sorry. I wrote it on the spot and made the same mistake I always make :P. Length is called Count on ArrayList objects. Why they chose a different name from builtin arrays in .Net has left me puzzled on more than one occasion.
Where myNumArray is an Array list and it just prints out System.Collections.ArrayList
which isnt very helpful.
Btw the final code look like this…for anyone else that need it.
function create_unique_random_array(desiredArrayLength,min,max) {
var list : ArrayList = new ArrayList();
list.Capacity=desiredArrayLength;
while (list.Count < desiredArrayLength)
{
var number :int= Random.Range(min, max);
if (!list.Contains(number))
{
list.Add(number);
}
}
return list;
}
Yeah, the only reason I remembered it off the top of my head is that I had to use it yesterday. I guess it makes sense, though, as ArrayLists are more likely to be used for non-fixed capacity add/remove applications, so length is a little more ambiguous term than the count/capacity dynamic.
var logString : String = "Nums: ";
for (var num : int in myNumArray) {
logString += num + ", ";
}
Debug.Log(logString);
function create_unique_random_array(desiredArrayLength,min,max) {
var list : ArrayList = new ArrayList();
list.Capacity=desiredArrayLength;
while (list.Count < desiredArrayLength)
{
var number :int= Random.Range(min, max);
if (!list.Contains(number))
{
list.Add(number);
}
}
return list;
}
How do you call this function and store its return value so that you can access the array in Uniscript?