A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Random Number Generator

  1. #1
    Junior Member
    Join Date
    Jan 2006
    Posts
    6

    Question Random Number Generator

    Hi,

    I'm pretty new to flash, and was trying to figure out how to create a random number generator from scratch. I've been trying to learn a bit of action scripting here and there, but I don't know if it's my level of understanding that just won't get it.

    I have the following...

    1)Input Text Field (to type in the max number that should be randomized to)
    Example: 60 (Number from 1 through 60 will be chosen)

    2) Dynamic Text Field (to show what random number has been generated).

    3) Button (to shuffle the numbers)

    Any assistance on how I should go about doing this would be great. I appreciate any help you can give me. Thanks.

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518

    Maybe you can adapt this to what you are trying to do...

    Code:
    stop();
    inputNum.text = "";
    outputNum.text = "";
    goBtn.onPress = doRandomize;
    function doRandomize() {
    	var numAmt = Number(inputNum.text);
    	if (numAmt > 0) {
    		var nums = new Array();
    		for (j = 1; j <= numAmt; j++) {
    			nums.push(j);
    		}
    		for (var i in nums) {
    			nums[i] = nums.splice(random(nums.length), 1, nums[i]);
    		}
    		outputNum.text = nums.pop();
    	} else {
    		trace("error");
    	}
    }
    Last edited by dawsonk; 08-21-2008 at 10:07 AM.

  3. #3
    Junior Member
    Join Date
    Jan 2006
    Posts
    6
    I'm sorry, this somewhat looks a bit Greek to me. I haven't had much experience with action scripting and so I'm not sure what needs to be done exactly. Based on what I'm seeing though, I'm guessing most of these procedures, but not sure if I really understand what's going on.
    Is it possible to break it down a bit so I know how this works.

  4. #4
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Re-read your original request, this is a much simplier version that will still give the same result...
    PHP Code:
    // Note: this code is on the main timeline
    // instance name of the input text field on the stage
    // text is set to blank
    inputNum.text "";
    // instance name of the output display text field on the stage
    // text is set to blank
    outputNum.text "";
    // instance name of movie clip (functioning as a button) on the stage
    // when the movie clip is clicked (onPress) it will do a custom function (doRandomize)
    goBtn.onPress doRandomize;
    // custom function (doRandomize) declared
    function doRandomize() {
        
    // create new user defined variable (numAmt)
        // set numAmt equal to the Number value of the inputNum textfield value
        
    var numAmt Number(inputNum.text);
        
    // check to make sure this is a number greater than 0
        
    if (numAmt 0) {
            
    // set the value of output display textfield
            // built in random function returns a value from 0 to one less than numAmt
            // add one, so value is now btween 1 and numAmt
            
    outputNum.text random(numAmt) + 1;
            
    // if numAmt was not greater than 0
        
    } else {
            
    // displays message in output window during authoring
            // this should be changed to display an on screen message for the user
            
    trace("error");
        }
        
    // end of function


  5. #5
    Junior Member
    Join Date
    Jan 2006
    Posts
    6
    Thanks Dawsonk.

    I went through some books and was able to "somewhat" figure out the action script. I really appreciate your help on this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center