27 Replies Latest reply on Nov 16, 2011 3:24 AM by kennethkawamoto2

# random numbers in a textfield?

hi all thx for reading and i hope you can help

I am creating a game for kids, which shows them how to tell the time

I need to put two random numbers into a textfield one for hours and one for minutes

so that I can get the app to say show me 13:45 for example

however I do not want to include the time of 12:00 as this is where the clock will start and defeats the purpose

the clock is 24 hour, going from 1-24 in hours

and 00-60 in minutes

ive looked everywhere on how to do this but cannot find an answer

at the moment i have a textfield which displays the time as the user adds hours etc which i plan to hide

so that i can say if this random number textfield is equal to the time textfield then display correct and so on

any help is appreciated

Thanks

fonzio

• ###### 1. Re: random numbers in a textfield?

It's not really clear what you are trying to do nor what you are having a problem with.  Do you have any code to show as an attempt?  What is the purpose of the textfields and what do random numbers have to do with them?  What is the user involvement as far as the user adding hours?  Where are kids taught to read time in 24 hour format?

• ###### 2. Re: random numbers in a textfield?

An example function  generates 24hr time string:

```function generateRandomTime():String {
var hour:uint = Math.floor(Math.random()*24); // creates a random number 0 - 23
var minute:uint = hour == 12 ? Math.floor(Math.random()*59) + 1 : Math.floor(Math.random()*60); // creates a random number 0 - 59, if the hour is 12 the number range is 1 - 59 (so that you don't get 12:00)
return hour + ":" + (minute < 10 ? "0" : "") + minute; // retun the result string
}

trace(generateRandomTime());
```

--

Kenneth Kawamoto

http://www.materiaprima.co.uk/

• ###### 3. Re: random numbers in a textfield?

excellent kenneth,

that does what i would like it to

one question though,

is there a way to make it not repeat itself,

lets say i run the code and i get 22:13

then i run again and get 22:13

is there a way to say if its the same, randomise again??

Fonzio

• ###### 4. Re: random numbers in a textfield?

incidentally

I had to add a +1 so that i didnt get a zero:

in the time

however now im worried i might get 25

would I?

thanks

fonzio

• ###### 5. Re: random numbers in a textfield?

@  Ned,

well i have a background, which changes from light to dark, denoting time, and a clock which reads, 1 to 12, and then changes after midday to 13 to 24, and so on

as a dynamic textfield, each time the user adds an hour the textfield will add one,

counting if you see what i mean

now if this is hidden, i need to match it up with what kenneth did,

you see the app will ask the question show me random : random time

and this random time will need to be checked against my dynamic textfield in order to get a match

hope this helps

fonzio

• ###### 6. Re: random numbers in a textfield?

No but you would get times such as 24:59. The code above generates a time between 0:00 - 23:59. What range do you want to generate?

--

Kenneth Kawamoto

http://www.materiaprima.co.uk/

• ###### 7. Re: random numbers in a textfield?

thats exactly the range then,

thanks very much for your help

fonzio

• ###### 8. Re: random numbers in a textfield?

is there a way to make it not repeat itself,

lets say i run the code and i get 22:13

then i run again and get 22:13

Flash (or computer in general for that matter) Math.random() cannot really generate a random number but only a pseudo-random number. That said it's interesting it generates exactly the same outcome consecutively. If I do:

```for(var i:uint = 0; i < 10; i++){
trace(generateRandomTime());
}
```

I get:

```23:59
13:11
1:56
1:37
23:55
15:36
3:30
7:32
10:06
1:53
```

So it's pretty random.

is there a way to say if its the same, randomise again??

Yes - but I don't like doing that, because in theory the function could run forever.

If you want to make sure you never  get the same time value, one way is to store all the time values in an Array, then randomise it, then pop one by one:

```// create an Array with numbers 0:00 - 23:59 in minutes, then randomise it
function generateRandomTimeList():Array {
var array:Array = new Array();
for(var i:uint = 0, end:uint = 60*24 - 1; i < end; i++){
array.push(i);
}
return FisherYates(array);
}

// Array randomising algorithm
function FisherYates(a:Array):Array {
var b:Array = new Array(a.length);
b[0] = a[0];
for(var i:uint = 1, n:uint = a.length; i < n; i++){
var j:uint = Math.round(Math.random()*i);
b[i] = b[j];
b[j] = a[i];
}
return b;
}

// convert number to time formatted string
function timeFormat(n:uint):String {
var hour:uint = Math.floor(n/60);
var minute:uint = n - hour*60;
return hour + ":" + (minute < 10 ? "0" : "") + minute;
}

// trace 10 random time
var randomTimeList:Array = generateRandomTimeList();
for(var i:uint = 0; i < 10; i++){
trace(timeFormat(randomTimeList[i]));
}
```

Trace

```11:24
23:01
9:04
10:55
13:17
9:28
16:06
8:41
7:26
21:05
```

--

Kenneth Kawamoto

http://www.materiaprima.co.uk/

• ###### 9. Re: random numbers in a textfield?

excellent work kenneth, i think i use the array then

is there a way to get that random time into a textfield called randomtime_txt.text rather than trace it?

and how would i check if it matched the textfield of mytime_txt.text

thanks for the help

much appreciated

Fonzio

• ###### 10. Re: random numbers in a textfield?

randomtime should be a variable

```mytime_txt.text == randomtime // true or false
```

--

Kenneth Kawamoto

http://www.materiaprima.co.uk/

• ###### 11. Re: random numbers in a textfield?

hi ken, yes i have that working correctly

one problem though is it is displaying 24:00 as 00

where can i add the +1 so that it goes from 1 to 24

thanks

Fonzio

• ###### 12. Re: random numbers in a textfield?

one problem though is it is displaying 24:00 as 00

where can i add the +1 so that it goes from 1 to 24

In essence you want 0:01 to 24:00, not 0:00 to 23:59, right? If so you can modify the initial array population:

```for(var i:uint = 1, end:uint = 60*24; i < end; i++){
```

--

Kenneth Kawamoto

http://www.materiaprima.co.uk/

• ###### 13. Re: random numbers in a textfield?

no sorry ken,

i have 1 to 12, then 13 to 24

there is no 0 as it may confuse the kids

thanks for any help

Fonzio

• ###### 14. Re: random numbers in a textfield?

Then simply substitute 0 with 24

```return (hour == 0 ? 24 : hour) + ":" + (minute < 10 ? "0" : "") + minute;
```

--

Kenneth Kawamoto

http://www.materiaprima.co.uk/

• ###### 15. Re: random numbers in a textfield?

thankyou ken, that did the job nicely

thanks again

fonzio

• ###### 16. Re: random numbers in a textfield?

Just for the fun of it, here is a one-line approach to generate random hours and minutes (including 24th hour replacement) - makes code much more abbreviated with no conditionals:

```for (var i:int = 0; i < 10; i++)
{
trace(new Date(1500000000000 * Math.random()).toString().match(/(?<=\d\s)\d+\:\d+/g)[0].replace(/^00/, "24").replace(/^0/, ""));
}
```
• ###### 17. Re: random numbers in a textfield?

return (hour == 0 ? 24 : hour < 10 ? "0" : "") +hour + ":"  + (minute < 10 ? "0" : "") + minute;

on another note,

i have ampm_txt

can i somehow add this to the above and get a conditional to work like

if (hours<12) {

ampm_txt.text="AM";

}

if (hours>12) {

ampm_txt.text="PM";

}

if (hours==24) {

ampm_txt.text="AM";

}

if (hours==12) {

ampm_txt.text="PM";

that would be really great

thanks

fonzio

• ###### 18. Re: random numbers in a textfield?

@Andrei

I thought about using Date object too first. But (1) shouldn't it be 1000*60*60*24 = 86400000 rather than 1500000000000 you have? (2) fonzio wants no duplication therefore you should create an Array of all the permutations first then randomise it. An Array of 86,400,000 Date objects is obviously too much but 60*24 = 1,440 Date objects would do.

--

Kenneth Kawamoto

htp://www.materiaprima.co.uk/

• ###### 19. Re: random numbers in a textfield?

randomTimeList in the code above contains numbers representing 0 minutes (0:00) to 1439 minutes (23:59) so you can test if a number is smaller than 720 minutes (12:00)

```ampm_txt.text = randomTimeList[i] < 60*12 ? "AM" : "PM";
```

(You don't have AM/PM with 24 hour clock though...)

--

Kenneth Kawamoto

http://www.materiaprima.co.uk/

• ###### 20. Re: random numbers in a textfield?

"shouldn't it be 1000*60*60*24 = 86400000 rather than 1500000000000 you have?"

It doesn't matter because time is expressed in milliseconds. Both values will do. I feel greater value leads to more random results.

"(2) fonzio wants no duplication therefore you should create an Array of all the permutations first then randomise it."

Randomizing milliseconds eliminates the need for array randomization. Granted, filtering out duplicates should be a separate task.

• ###### 21. Re: random numbers in a textfield?

It doesn't matter because time is expressed in milliseconds. Both values will do. I feel greater value leads to more random results.

Not really - you'll get biased results if the number is not multiple of 86,400,000; and there is no point in using more than 86,400,000. The goal is to produce a random time string out of 1440 possibilities, which is different from producing random time.

Randomizing milliseconds eliminates the need for array randomization.

You are not eliminating duplication possibilities, which is the reason for Array randomisation.

--

Kenneth Kawamoto

http://www.materiaprima.co.uk/

• ###### 22. Re: random numbers in a textfield?

I disagree although a little bias may be present. on a very large number it is negligible. As for the number manipulation vs performance - there is no difference so I would deal with as large number as possible. Multiples of 86400000 would help a little of course. So, one can use 1499990400000 instead of 1500000000000. By the way, the difference between these two number is only 0.0006399999999961992% - negligible.

"You are not eliminating duplication possibilities, which is the reason for Array randomisation."

Did I say I was eliminating duplicates?

Array randomization does not eliminate duplicates per se. So, additional Array randomization is an overkill.

• ###### 23. Re: random numbers in a textfield?

If you read the thread you'll see a randomised array has been used to avoid duplicate. If you use yours (or my original code) the chance of getting the duplicate is relatively high - 1 in 1440  to get the next as a duplicated result, for a simple example. Also the probability for getting any one time string is always 1/1440 no matter how large the number you use for Date() (although the bias gets smaller and smaller and all probabilities will get close to 1/1440 as the number grows - if you do not use multiple of 86400000 that is, if you do there's no bias so might as well just use 86400000.)

--

Kenneth Kawamoto

http://www.materiaprima.co.uk/

• ###### 24. Re: random numbers in a textfield?

thanks ken, i only want to randomize 5 times then the game will end and on to the next one

thanks again

fonzio

• ###### 25. Re: random numbers in a textfield?

That's fine - just pick up the first 5 from the randomised array, there's zero chance of getting the same time combinations

--

Kenneth Kawamoto

http://www.materiaprima.co.uk/

• ###### 26. Re: random numbers in a textfield?

thanks ken

incidentally the next game is a sequence game using numbers

id like to use the same code if possible,

so in level 1 the main number (3) would randomize, and then three others would appear which would need to be put in order (like in a jigsaw)

as in 3, 5, 7, 9

randomize to 10, as in 10, 20, 30, 40

wondering if this is possible and how i would need to go about it?

thanks

fonzio

• ###### 27. Re: random numbers in a textfield?

First you need to generate an Array with numbers in sequence. You'll need to have (a) the base number, (b) the increment, and (c) the length. If we take your first example, the base number is 3,  the increment is 2, and the length is 4. The code will be something like:

```var baseNumber:uint = 3;
var increment:uint = 2;
var length:uint = 4;
var numberSequenceArray:Array = new Array();
for(var i:uint = 0; i < length; i++){
numberSequenceArray.push(baseNumber);
baseNumber += increment;
}
trace(numberSequenceArray);
// tarce
// 3,5,7,9
```

Once you have the array you can randomise the order by using the same function as before.

--

Kenneth Kawamoto

http://ww.materiaprima.co.uk/