AS3 Concatenate integers

Posted on 2009-02-14
Last Modified: 2013-11-11

I'm trying to concatenate three different integers to make a string with the code below. However I'm getting the following error.

1067: Implicit coercion of a value of type Number to an unrelated type String.

Can anyone tell me how to do this?

Also I have very little knowledge of AS are the functions correct to produce; Random Number between 0-99 - Seconds since 01/01/1970 - Random Number between 0-99? for example...

If part 1 came back as 45, part two came back as 1234567890 and part 3 came back as 12 I would want the uniqueKey var to be 45123456789012 NOT 1234567947
var now:Date = new Date();

var uniqueKey:String = (Math.random()*99) + now.getTime() + (Math.random()*99);

Open in new window

Question by:Mr_Splash
    LVL 19

    Accepted Solution


    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="" layout="absolute" creationComplete="init()">
    			private function init():void{
    				var now:Date = new Date();
    				var uniqueKey:String = (Math.random()*99).toString() + "--" + now.getTime().toString()  + "--" +  (Math.random()*99).toString();
    				textField.text = uniqueKey;
    	<mx:Text id="textField" x="95" y="253" />

    Open in new window

    LVL 8

    Assisted Solution

    try this...

    var now:Date = new Date();
    // use .toString() to avoid addition
    var uniqueKey:String = (Math.round(Math.random()*99)).toString() +" - "+ (now.getTime()).toString() +" - "+ (Math.round(Math.random()*99)).toString();
    //1) Generate random number : 
    var rn:Number = Math.random()*99
    trace("rn = "+rn);
    var addition:Number = rn+rn;
    var concat:String = rn.toString() + rn.toString();
    trace("addition = "+addition);
    trace("concat = "+concat);
    // Should both trace the same
    trace("rn.toString() = "+ rn.toString());
    trace("rn =  "+ rn);
    trace("Math.round(rn) = "+Math.round(rn));
    trace("Math.floor(rn) = "+Math.floor(rn));
    trace("Math.ceil(rn) = "+Math.ceil(rn));
    trace("uniqueKey = "+uniqueKey);

    Open in new window

    LVL 3

    Author Closing Comment

    Thanks guys, I've split the points as both solutions were pretty much the same and were posted at the same time.

    This is all that was needed in the end.
    var uniqueKey:String = (Math.round(Math.random()*99)).toString() + "" +(now.getTime()).toString() + "" + (Math.round(Math.random()*99)).toString();

    Please let me know if there is something incorrect with that.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
    I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
    In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
    The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now