Skip to main content
Inspiring
February 10, 2017
Answered

Stop math.random generating numbers

  • February 10, 2017
  • 3 replies
  • 791 views

Hi,

I have made a test which has a "test over" page at the end. When the test has finished and goes to this page, I cannot get the buttons to work - even though it was using a code snippet. I have noticed that math.random() keeps generating numbers when I get to the "test over" page and wondered if this was the reason why I could not get any buttons to work.

Is there a way to stop math.random() from generating numbers? The code for it is;

var MeSelRam: Number = Math.random();

MeSelectQu = Math.round(MeSelRam * 9 + 1);

Numbers are generated on frame 5. "Test over" is frame 111.

On the "test over" frame I tried to use MeSelRam = null; but didn't work.

This topic has been closed for replies.
Correct answer jackh8500410

I've just managed to fix it. I put the math.random() inside of my if statement;

if (MeAnsweredQu == 10) {

  gotoAndStop("MeTestOver")

} else {

  //Selecting random number

  var MeSelRam: Number = Math.random();

  MeSelectQu = Math.round(MeSelRam * 9 + 1);

So now it will only generate if not all 10 questions have been answered. Thanks for both your help

Jack

EDIT: By doing this, it has also allowed my buttons to work on the test over frame!

3 replies

jackh8500410AuthorCorrect answer
Inspiring
February 10, 2017

I've just managed to fix it. I put the math.random() inside of my if statement;

if (MeAnsweredQu == 10) {

  gotoAndStop("MeTestOver")

} else {

  //Selecting random number

  var MeSelRam: Number = Math.random();

  MeSelectQu = Math.round(MeSelRam * 9 + 1);

So now it will only generate if not all 10 questions have been answered. Thanks for both your help

Jack

EDIT: By doing this, it has also allowed my buttons to work on the test over frame!

kglad
Community Expert
Community Expert
February 10, 2017

you're welcome.

kglad
Community Expert
Community Expert
February 10, 2017

the following answers your question.  but before employing one of the two snippets read message 1 because there may be some other issue more fundamental than your question indicates.

if you have code in a frame that you only want to execute once AND you need to replay that frame repeatedly, use either:

// 1.

var alreadyExecuted:Boolean;

if(!alreadyExeucted){

alreadyExecuted=true

oneTimeF();

}

function oneTimeF():void{

// the code you want to execute once

}

// or 2.

var alreadyExecuted:Boolean;

if(!alreadyExeucted){

alreadyExecuted=true

// put the code you want to execute once here

}

Legend
February 10, 2017

Uh... if you don't want that code to execute more than once, then don't loop to that frame more than once?

That being said, I cannot fathom why you'd think generating a random number would break your buttons. Unless the values of MeSelRam and MeSelectQ are explicitly involved in your button event handler logic, there's no way it's having any effect on them.