• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

random numbers in a textfield?

Community Beginner ,
Nov 02, 2011 Nov 02, 2011

Copy link to clipboard

Copied

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

how would i go about this?

any help is appreciated

Thanks

fonzio

TOPICS
ActionScript

Views

4.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 07, 2011 Nov 07, 2011

Then simply substitute 0 with 24

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

--

Kenneth Kawamoto

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

Votes

Translate

Translate
LEGEND ,
Nov 02, 2011 Nov 02, 2011

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 03, 2011 Nov 03, 2011

Copy link to clipboard

Copied

@  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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 03, 2011 Nov 03, 2011

Copy link to clipboard

Copied

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/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 03, 2011 Nov 03, 2011

Copy link to clipboard

Copied

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??

thanks for your help

Fonzio

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 03, 2011 Nov 03, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 03, 2011 Nov 03, 2011

Copy link to clipboard

Copied

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/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 03, 2011 Nov 03, 2011

Copy link to clipboard

Copied

thats exactly the range then,

thanks very much for your help

fonzio

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 03, 2011 Nov 03, 2011

Copy link to clipboard

Copied

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 = b;

         b = a;

      }

   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));

}

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/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 03, 2011 Nov 03, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 04, 2011 Nov 04, 2011

Copy link to clipboard

Copied

randomtime should be a variable

mytime_txt.text == randomtime // true or false

--

Kenneth Kawamoto

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 06, 2011 Nov 06, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 07, 2011 Nov 07, 2011

Copy link to clipboard

Copied

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/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 07, 2011 Nov 07, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 07, 2011 Nov 07, 2011

Copy link to clipboard

Copied

Then simply substitute 0 with 24

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

--

Kenneth Kawamoto

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 07, 2011 Nov 07, 2011

Copy link to clipboard

Copied

thankyou ken, that did the job nicely

thanks again

fonzio

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 07, 2011 Nov 07, 2011

Copy link to clipboard

Copied

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/, ""));

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 08, 2011 Nov 08, 2011

Copy link to clipboard

Copied

@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/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 08, 2011 Nov 08, 2011

Copy link to clipboard

Copied

"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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 08, 2011 Nov 08, 2011

Copy link to clipboard

Copied

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/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 08, 2011 Nov 08, 2011

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 08, 2011 Nov 08, 2011

Copy link to clipboard

Copied

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/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 14, 2011 Nov 14, 2011

Copy link to clipboard

Copied

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

thanks again

fonzio

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 15, 2011 Nov 15, 2011

Copy link to clipboard

Copied

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/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 16, 2011 Nov 16, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines