The McLeodGaming forums were permanently closed on April 30th, 2020. You are currently viewing a read-only archive.
View unanswered posts | View active topics It is currently Fri May 15, 2020 5:40 am

Forum rules


IMPORTANT

This Forum is for Flash related questions ONLY! Do not ask questions about spriting, Game Maker, Photoshop, or anything else not related to Flash.



 [ 26 posts ]  Go to page 1, 2  Next
[Question] Random choices 
Author Message
User avatar

Joined: Thu Mar 05, 2009 10:49 am
Posts: 31
Hi guys so I'm trying to do a test on math.random and I need the fla to change the score to one when the last sword thing is pressed, but it's not working and I really don'w know why. I'm posting the code and all of that. I really need help. If you need to contact me here's my email:[email protected] in advance.

_________________
Image
Click the pod to go to my website.


Sun May 03, 2009 2:13 pm
User avatar

Joined: Wed Apr 29, 2009 11:28 pm
Posts: 323
Location: Nowhere, Antarctica
Gender: Anime Girl
Skype: Evilagram
I have no idea what on earth you're trying to do. Could you try being more clear?

If you need to set the sorce to one, just say, on(release){score = 1;}

_________________
Image

Style [Stayl] (n) - One's unique and personal method of defacing a perfectly good piece of paper.

READ THIS: http://ipgd.freehostia.com/copypasta.html


Sun May 03, 2009 3:35 pm
User avatar

Joined: Thu Mar 05, 2009 10:49 am
Posts: 31
I need a number for 1 to 3 randomly. I tryed using math.random but it didn't work.Is the attachement working? If not tell me I'll give you a link to it. If you need anything else just ask.

_________________
Image
Click the pod to go to my website.


Sun May 03, 2009 4:15 pm
User avatar

Joined: Mon Aug 11, 2008 1:09 pm
Posts: 166
Location: England lol
Gender: Male
Currently Playing: Minecraft
myNum = random(3)+1; should work, its not the best method but it'll do. Cue Evilagrams algorithm for Math.random() method...


Sun May 03, 2009 4:31 pm
WWW
User avatar

Joined: Thu Mar 05, 2009 10:49 am
Posts: 31
Thanks but I have a question why to I need to write +1? I only need 3 numbers.
It still doesn't work.

_________________
Image
Click the pod to go to my website.


Sun May 03, 2009 6:28 pm
User avatar

Joined: Wed Apr 29, 2009 11:28 pm
Posts: 323
Location: Nowhere, Antarctica
Gender: Anime Girl
Skype: Evilagram
Ow. That's not how Math.Random works at all.

If you want a number equal to exactly 1, 2, or 3, then the correct method would be:

Score = Math.round(Math.random()*3+1);

If you want it to add to the current score, do this:

Score = Score + Math.round(Math.random()*3+1);


To explain the Math.random method a bit, it randomly generates an irrational number from 0 to 1. Just multiply by the range which you want (1-3 has a range of three) and add to the lowest number of the range (1 in this case). Math.round rounds the number to the neared value.

_________________
Image

Style [Stayl] (n) - One's unique and personal method of defacing a perfectly good piece of paper.

READ THIS: http://ipgd.freehostia.com/copypasta.html


Sun May 03, 2009 9:47 pm
User avatar

Joined: Mon Aug 11, 2008 1:09 pm
Posts: 166
Location: England lol
Gender: Male
Currently Playing: Minecraft
Agreed that your Math.random() method works better but I think you'll find that writing random(5); for example will in fact generate a random number between 0 and 4, then add one will make it between 1 and 5. Try it.


Mon May 04, 2009 11:34 am
WWW
User avatar

Joined: Wed Apr 29, 2009 11:28 pm
Posts: 323
Location: Nowhere, Antarctica
Gender: Anime Girl
Skype: Evilagram
No it doesn't. It just generates a number between 1 and 0. Have you tried your own code?

_________________
Image

Style [Stayl] (n) - One's unique and personal method of defacing a perfectly good piece of paper.

READ THIS: http://ipgd.freehostia.com/copypasta.html


Mon May 04, 2009 11:37 am
User avatar

Joined: Mon Aug 11, 2008 1:09 pm
Posts: 166
Location: England lol
Gender: Male
Currently Playing: Minecraft
Evilagram wrote:
No it doesn't. It just generates a number between 1 and 0. Have you tried your own code?


Okay copy and paste this into the movies first frame and run it multiple times:

Code:
trace(random(5)+1);


Now tell me it doesn't generate a random number from 1 to 5 - I use it all the time when I don't want to get too complicated with the random numbers so I know it works!


Mon May 04, 2009 11:48 am
WWW
User avatar

Joined: Wed Apr 29, 2009 11:28 pm
Posts: 323
Location: Nowhere, Antarctica
Gender: Anime Girl
Skype: Evilagram
Wow, that's exactly the code I wrote. I put it on two frames, so it would loop and run every frame. Then I tested the movie. Here's the output:

Code:
1.83251965651289
1.05784177687019
1.82829308742657
1.09476363891736
1.51326166000217
1.76205865899101
1.47127442341298
1.81924107857049
1.21436914987862
1.33759195217863
1.1698354515247
1.60859565623105
1.12506395811215
1.64765789406374
1.04340737732127
1.377713478636
1.71885367482901
1.65411084704101
1.18565026810393
1.69855040498078
1.64936943724751
1.60159979714081
1.09117275243625
1.55072051892057
1.96043380629271
1.15133056230843
1.58936345484108
1.23770548310131
1.39707113429904
1.97875383356586
1.25028646877035
1.43414258677512
1.94073739554733
1.46266365004703
1.79210716485977
1.00096549233422
1.28844895213842
1.7744080144912
1.99712839070708
1.87190171238035
1.99788088537753
1.51939338818192
1.74242471903563
1.45953326532617
1.09379816940054
1.82515385374427
1.62607746338472
1.99667170923203
1.93607832165435
1.28340175841004
1.5627880031243
1.17103869095445
1.77476005861536
1.90781844360754
1.36015161452815
1.7965395511128
1.99270281288773
1.73701670859009
1.30332432501018
1.71065655397251
1.75418890966102
1.70504831476137
1.36256452091038
1.47751034144312
1.48008329980075
1.96622215397656
1.77058479189873


The code used was as follows

Code:
trace(Math.random(5)+1);


Funny. It's not working as advertised.

Here's the documentation on the random method:

http://www.adobe.com/support/flash/acti ... ry466.html

There are no parameters for the random method. If you think I screwed up, here's the fla:

http://dl.getdropbox.com/u/416986/temp/RandomTest.fla

_________________
Image

Style [Stayl] (n) - One's unique and personal method of defacing a perfectly good piece of paper.

READ THIS: http://ipgd.freehostia.com/copypasta.html


Mon May 04, 2009 11:56 am
User avatar

Joined: Mon Aug 11, 2008 1:09 pm
Posts: 166
Location: England lol
Gender: Male
Currently Playing: Minecraft
Sorry but you are obviously misreading what I write, I wrote this
Code:
trace(random(5)+1);

Not this
Code:
trace(Math.random(5)+1);


random() and Math.random are two different things.


Mon May 04, 2009 11:58 am
WWW
User avatar

Joined: Wed Apr 29, 2009 11:28 pm
Posts: 323
Location: Nowhere, Antarctica
Gender: Anime Girl
Skype: Evilagram
Weeeeeeeelllllll, f***. You quoted code from flash 4.

http://www.adobe.com/support/flash/acti ... ry630.html

Quote:
This function is deprecated in Flash 5; use of the Math.random method is recommended.


It's a faulty implementation. Perhaps it generates less random numbers or lags more, f*** if I know. One thing for certain is that it can only generate integers, not irrational numbers, meaning that it's useless for a number of applications.

_________________
Image

Style [Stayl] (n) - One's unique and personal method of defacing a perfectly good piece of paper.

READ THIS: http://ipgd.freehostia.com/copypasta.html


Mon May 04, 2009 12:17 pm
User avatar

Joined: Mon Aug 11, 2008 1:09 pm
Posts: 166
Location: England lol
Gender: Male
Currently Playing: Minecraft
Which is why I agreed with you that the Math.random method is far more efficient, but from experience with using flash for random number generating it works better for random numbers that are hidden behind the scenes - for example I use it to make events happen randomly like so:

Code:
if(random(100) < 10) {
  //Do Something
}


Works well enough for me and for a majority of users here learning flash it would probably work just as well too, but I guess teaching them the modern techniques will be better for them in the long run rather than using deprecated code all the time.


Mon May 04, 2009 12:26 pm
WWW
User avatar

Joined: Wed Apr 29, 2009 11:28 pm
Posts: 323
Location: Nowhere, Antarctica
Gender: Anime Girl
Skype: Evilagram
I just do,

Code:
if(Math.random() < .5){
//s*** happens
}


EDIT: Ought to say more than that. Because it has a wider range and crap, it's more random, or something along those lines.

_________________
Image

Style [Stayl] (n) - One's unique and personal method of defacing a perfectly good piece of paper.

READ THIS: http://ipgd.freehostia.com/copypasta.html


Mon May 04, 2009 12:50 pm

Joined: Mon Aug 11, 2008 11:33 am
Posts: 22
Try this code.I think is best than x21 and evilgram,but is a bit more complicated.

Code:
Math.round(Math.random()*3+1)


Tue May 05, 2009 12:36 am
Display posts from previous:  Sort by  
 [ 26 posts ]  Go to page 1, 2  Next

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software for PTF.