Flash As3 random selected Disks plus Chance/luck - actionscript-3

My question is how can i add chances to the random selected disks in Actionscript3.0.
SO if the player presses the disks button he/she gets 30% chance to roll disk7 and 40% on disk6. How can i pull this trough.
disks.addEventListener(MouseEvent.MOUSE_DOWN, hitDisk);
function hitDisk(event:MouseEvent):void{
disks.gotoAndStop(Math.ceil(Math.random()*7));
}
Disks[movieclip] > haves 7 keyframes with disks inside of it.
The final frame is the special disk. Every disk haves a instance name.
Disks[movieclip] > (frame1)diskOne, (frame2)diskTwo, (frame3)diskThree.....and so on.
(disks are just like dices but round shaped)
Some help would be nice!!! Thanks!!!

I think you want something like this:
function hitDisk(event:MouseEvent):void{
var rand:int = Math.random() * 100; //gives you a number between 0 and 100
if (rand <= 40) //40% chance
disks.gotoAndStop(7);
} else if (rand > 40 && rand <= 70) { //30% chance
disk.gotoAndStop(6);
} else { //30 % chance
disk.gotoAndStop(5);
}

If you search up ‘random number generators with a bias’ or ‘adding a bias to a random number generator’ (something along these lines) you should find ways to do what you want: to have certain numbers in your random number ‘rolling’ algorithm produce certain numbers more then others.
I’m going to make an assumption here that a disc is like a die, but with seven possible outcomes.
This is one basic method I've used in the past for a similar problem.
You mentioned:
30% chance to roll 7
40% chance to roll 6
I’m going to assume then that 1,2,3,4,5 have an equal chance in the missing 30% chance, so 30% divided by 5 is 6% chance for all the other numbers. So:
30% chance to roll 7
40% chance to roll 6
6% chance to roll 5
6% chance to roll 4
6% chance to roll 3
6% chance to roll 2
6% chance to roll 1
So given these percentages (totaling 100%), you could make an array with 100 spaces and:
fill the first 6 indexes with ‘1’
fill the next 6 indexes with ‘2’
fill the next 6 indexes with ‘3’
fill the next 6 indexes with ‘4’
fill the next 6 indexes with ‘5’
fill the next 40 indexes with ‘6’
fill the next 30 indexes with ‘7’
Then use Math.random() to choose an index at random from this array. Since there are 40 chances out of 100 for ‘6’ to be picked, ‘6’ would come out 40% of the time. And so on with the other numbers.
This can be pretty wasteful to have a large array just for this, but you can improve this with some math (right away I can see that the array can actually be half the size and still contain the desired percentages, just by dividing everything by 2). Or you could take advantage of the fact that numbers 1,2,3,4,5 have an equal outcome and separate it out resulting in an even smaller array (just to calculate the biased outcomes), and so forth.

Related

Halcon - speed up Intersection

I have a XLD contour, for semplicity imagine a circle with a hole, like a doughnut... (in reality its a organic shape.. its just to simplify...)
I draw lines across this circle at a certain angle, and need to find the two outermost intersections of the lines with the circle. I do it like this:
gen_contour_polygon_xld (Line,[curYL,curYR],[x1,x2])
intersection_contours_xld(Line,CircleXld,'all',Y,X,overlapping)
and then pick the first and last values from the Y and X tuples.
The intersection_contours_xld takes 2.39 ms on average.. I need to run this 15 times per picture, and have a total of 6 pictures. Is there a faster way to do this? It is eating up too much time...
this one is much faster with only 0.011ms..
intersection_line_contour_xld(CrateRegionXld,curYL,x1,curYR,x2,Y,X,overlapping)

Forcing Column Headings in SSRS Chart

I'm struggling with charting some data in SSRS with a column chart. The data in my SQL select returns a number of devices and their battery level as a decimal e.g
Device 1, 90%
Device 2, 90%
Device 3, 80%
Device 4, 30%
I have rounded the real battery percentage so the view can only ever output, 100 to 0 in 10% chunks.
I then have a column chart in SSRS which counts the number of devices which fall into each 10% and charts them. The issue is that in this example only the columns for 30%, 80% and 90% appear, along with their headings. What I would like is to show all of the titles from 0 to 100 but only draw a column in where there is a record.
I've tried forcing the scale with 10 increments but that doesn't help. I guess this question is two fold. Is there a way that I can force SSRS to display the column headers even though there is no data for those specific headers? Any other way that this can be achieved if not?
I have a feeling that I might need to do something in the select to force the data to include these but not sure how I might do that.
Here is an example of the chart currently:
As you can see, I only get column titles for 30, 80 and 90.

Aligning primary and secondary axis in charts in ssrs

I have a chart where I am plotting numbers and percentages (in the y-axis) and the corresponding month values in the x-axis. The number values vary from 0 to 30000 based on the months. The interval I have set is 2000 for the number and 10 for the percentage.
The number is plotted on primary axis and the percentage on the secondary axis.
The problem I am having is aligning the axis. I am not able to post the screenshot as apparently I don't have reputation enough to post images.
but this screenshot is similar to the problem I am facing.
screenshot http://www.allaboutmssql.com/2013/08/ssrs-chart-with-two-vertical-y-axes.html
Old question, but I ran into this today and thought I'd share what I did. As Jonnus suggested in the comments, I set both axes to have the same number of intervals (in my case, 5). I also rounded the number axis upwards, so the 5 dynamic intervals would always be a round number (e.g., 28,000) and not an oddly precise number (e.g., 28,543). I did this by setting the min, max, and interval properties for each axis in turn.
For the percentage axis, I set the minimum to be 0%, the maximum to be 100%, and the interval to be 20%. This guarantees 5 intervals above 0 (20%, 40%, 60%, 80%, 100%). In my scenario, it was a 100% stacked column chart, so they'd never be more than 100% and I could hard-code these numbers.
The number side was more challenging for me because I wanted the axis to scale dynamically based on the data. If that isn't a requirement for you, you can hard-code the min to 0, the max to your chosen number, and the interval to be the max / 5 (e.g., 0 min, 250,000 max with 25,000 intervals).
To make the number axis dynamic, it's the same principal, but I used an expression instead of hardcoding the numbers. I set the max to be whatever value comes in, rounded upwards to the next 10,000.
Max expression:
=Ceiling((Max(Fields!YourFieldName.Value, "YourDatasetName")) / 10000) * 10000
You can change the 10,000 to any interval that makes sense for your data (10, 100, 1000, 1m, etc.)
The interval formula is the same as the max expression, divided by 5:
=(Ceiling((Max(Fields!YourFieldName.Value, "YourDatasetName")) / 10000) * 10000) / 5
With these expressions, if the max value is 92,000, I round the max axis to 100,000, then divide that by 5 to give me 5 intervals of 20,000 each. If the max value is 141,000, I round the max to 150,000, divide by 5 to give me 5 intervals of 30,000. This is all dynamic.
Since both axes have 5 intervals, the gridlines always align. Both axes can be hard-coded, both can be dynamic, or you can have a mix - as long as the number of intervals is the same.
Speaking more generally, it's always a good idea to be sure a secondary axis is appropriate (vs. a separate chart). It's easy for people to read into the placement of a line on a column chart (say), even though the placement is controlled by the scale of the axes and tells you nothing about the data itself.

Shadow that appears as ball gets closer to ground

I have developed a simple basketball game. The ball can be thrown and bounces off the ground, walls, rim, and backboard. I would like to have a ball shadow appear when the ball gets close to the ground. I've done my research and am coming up with nothing. So far I have had a limited amount of success with the following, running in the update loop of course:
shadow.alpha = _activeBall.y/600;
shadow.x = _activeBall.x;
600 is the floor, the maximum y value the ball can fall. The code above gets me close, but the shadow is always present, even when I am high enough in the air that a shadow should not be seen. I tried something like this:
if( _activeBall.y > 450 ) shadow.alpha = _activeBall.y/600;
shadow.x = _activeBall.x;
but that pops the shadow in to abruptly. It would also be ideal if the scale of the shadow decreased as the ball moved away from the floor. I am stumped with the math with this one and was hoping someone here can recommend an approach for this. Come on math gurus! Whatcha got?
Ok, so you need to learn the skill of normalizing / rescaling. It's not tough, as long as you know what you want.
It looks like you want alpha to be 0 when _activeBall.y < 450, then go from something less than 450/600 to eventually 600/600. Since I don't know what you want your alpha to start at (0 or 0.25 seems logical), I'll just call it alpha0. The good news for algebra - it lets you use a variable. :-)
So, the first easy part:
if( _activeBall.y < 450 ) { shadow.alpha = 0; }
// I ALWAYS use {} in any control flow statement.
So, normalize the range you want - turn it into a number from 0 to 1. Do this by subtracting the initial value and multiplying by the total range.
var normalized = (_activeBall.y - 450) / (600 - 450);
Note the (), otherwise you'd calculate _activeBall.y - (450/150).
Now that you have the normalized value, apply it to your scale by doing the opposite function with your new scale - multiply it times the total range and add the initial value:
shadow.alpha = normalized * (1 - alpha0) + alpha0;
Incidentally, you can do similarly with the scale of the shadow, so that when it's higher up, it's smaller. The normalized value will still be the same, and you can just set the scaleX and scaleY with that value.

How to make randomized numbers but if they are too close, change it

I want to make randomized numbers but if they are too close, I want to make it a reroll or make it a number further away from the the other number or try to make it a little more spread out.
I guess I am not sure what exactly I want :/
Thanks in advance
EDIT: So the reason i am making randomized numbers is for spawning positions for units on sides of the screen so i want units to not be too close to each other essentially to make it look better.
I want to make randomized numbers but if they are too close, I want to
make it a reroll or make it a number further away from the the other
number or try to make it a little more spread out.
A random number generator sometimes produces numbers that are near each other.
If you don't want them so close together, maybe what you need is fixed numbers each with a small random perturbation.
Let's say you choose numbers 10, 20, 30, 40, and 50. Then, you run a random number generator that gives you a number between -3 and +3. You add this random number to each one in turn. This produces numbers that aren't uniform, but they're not too close to one another.
If a re-roll would be sufficient, you could just save the previous value, and then on the next iteration, if the difference between the two numbers is too small, call Math.rand() again. Maybe your sample space is too small?
Get random position
If it's not good (too close to some other or what ever) go to step 1.
There you go! You got valid position. :)
...
Or...pre-calculate all valid positions, place them in some array and use rnd function to get array indexes - what array elements will use. That should be the faster solution (if you have huge number of units)...