Skip navigation
fonzio1
Currently Being Moderated

random numbers in a textfield?

Nov 2, 2011 7:07 PM

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

 
Replies
  • Currently Being Moderated
    Nov 2, 2011 7:48 PM   in reply to fonzio1

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 3, 2011 6:16 AM   in reply to fonzio1

    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/

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 3, 2011 9:57 AM   in reply to fonzio1

    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/

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 3, 2011 10:55 AM   in reply to fonzio1

    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/

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 4, 2011 4:15 AM   in reply to fonzio1

    randomtime should be a variable

    mytime_txt.text == randomtime // true or false
    

    --

    Kenneth Kawamoto

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

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 7, 2011 2:00 AM   in reply to fonzio1

    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/

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 7, 2011 6:29 AM   in reply to fonzio1

    Then simply substitute 0 with 24

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

    --

    Kenneth Kawamoto

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

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 7, 2011 9:12 AM   in reply to fonzio1

    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/, ""));
    }
    
     
    |
    Mark as:
  • Currently Being Moderated
    Nov 8, 2011 2:12 AM   in reply to Andrei1

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

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 8, 2011 3:02 AM   in reply to fonzio1

    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/

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 8, 2011 6:02 AM   in reply to kennethkawamoto2

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

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 8, 2011 6:28 AM   in reply to Andrei1

    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/

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 8, 2011 7:45 AM   in reply to kennethkawamoto2

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 8, 2011 8:44 AM   in reply to Andrei1

    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/

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 15, 2011 2:00 AM   in reply to fonzio1

    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/

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 16, 2011 3:24 AM   in reply to fonzio1

    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/

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points