2 Replies - 759 Views - Last Post: 07 March 2018 - 10:38 AM

#1 Asterisk_Angelo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-March 18

Flash AS3 Random Non Repetitive Text Generator

Posted 07 March 2018 - 10:30 AM

import flash.events.MouseEvent;


var names:Array = ["name1", "name2","name3", "name4", "name5"];

generate.addEventListener(MouseEvent.CLICK,generateName);
function generateName(event:MouseEvent): void{
var randomSurname= names[int(Math.random() * names.length)];
result.text=randomSurname.toString();
}
:code:

I am newbie in coding, I just recently started learning Adobe Flash AS3, I was told to stop learning it and try to learn other power application and programming language, for it was outdated, but I still insist to try it first.

I have been working on simple Random Text Generator where in output must not be repetitive. My Code above works just fine if Only I can restrict it from generating names are already been generated. I was told to use conditional operations. The problem was that i dont know where to start and what to do. Thank you and i appreciated the help I could get.

This post has been edited by modi123_1: 07 March 2018 - 10:35 AM
Reason for edit:: In the future please use the [code] tag button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: Flash AS3 Random Non Repetitive Text Generator

#2 Asterisk_Angelo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-March 18

Re: Flash AS3 Random Non Repetitive Text Generator

Posted 07 March 2018 - 10:34 AM

import flash.events.MouseEvent;


var names:Array = ["name1", "name2","name3", "name4", "name5"];

generate.addEventListener(MouseEvent.CLICK,generateName);
function generateName(event:MouseEvent): void{
var randomSurname= names[int(Math.random() * names.length)];
result.text=randomSurname.toString();
}


I am newbie in coding, I just recently started learning Adobe Flash AS3, I was told to stop learning it and try to learn other power application and programming language, for it was outdated, but I still insist to try it first.

I have been working on simple Random Text Generator where in output must not be repetitive. My Code above works just fine if Only I can restrict it from generating names that are already been generated.By the way I used a button (generate) and dynamic text box (result). I was told to use conditional operations. The problem was that i dont know where to start and what to do. Thank you and i appreciated the help I could get.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14569
  • View blog
  • Posts: 58,424
  • Joined: 12-June 08

Re: Flash AS3 Random Non Repetitive Text Generator

Posted 07 March 2018 - 10:38 AM

If you are using this as your index: int(Math.random() * names.length) why wouldn't you expect it to go past the length of the array? Think about it. If you array has 5 items in it.. and the random number is 10.. then the code will try to find something at index 50 which is way out of bounds.

Typically 'mod' or '%' helps limit a number.

.. and yes, Flash is going away. Security, overhead, and better options are the main reasons.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1