Posted By

adrianparr on 02/07/10


Tagged

random as3 utils gskinner rnd


Versions (?)

Who likes this?

7 people have marked this snippet as a favorite

Demian
MacEvelly
jhoard
kojok
Nikolai
tspitzr
letmein_xxi


AS3 Random Methods Utility Class


 / Published in: ActionScript 3
 

URL: http://www.gskinner.com/blog/archives/2008/01/source_code_ran.html

This handy class for working with random values was written by Grant Skinner. I take no credit for it.

A zip file containing 'Rnd.as' can be downloaded from here ... http://www.gskinner.com/blog/assets/Rnd.zip

  1. /**
  2. * Rnd by Grant Skinner. Jan 15, 2008
  3. * Visit www.gskinner.com/blog for documentation, updates and more free code.
  4. *
  5. *
  6. * Copyright (c) 2008 Grant Skinner
  7. *
  8. * Permission is hereby granted, free of charge, to any person
  9. * obtaining a copy of this software and associated documentation
  10. * files (the "Software"), to deal in the Software without
  11. * restriction, including without limitation the rights to use,
  12. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following
  15. * conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be
  18. * included in all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  22. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  24. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  25. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  27. * OTHER DEALINGS IN THE SOFTWARE.
  28. */
  29.  
  30. package com.gskinner.utils {
  31.  
  32. // Utility class provides common random functions.
  33.  
  34. public class Rnd {
  35. // static interface:
  36. // random(); // returns a number between 0-1 exclusive.
  37. public static function random():Number {
  38. return Math.random();
  39. }
  40.  
  41. // float(50); // returns a number between 0-50 exclusive
  42. // float(20,50); // returns a number between 20-50 exclusive
  43. public static function float(min:Number,max:Number=NaN):Number {
  44. if (isNaN(max)) { max = min; min=0; }
  45. return random()*(max-min)+min;
  46. }
  47.  
  48. // boolean(); // returns true or false (50% chance of true)
  49. // boolean(0.8); // returns true or false (80% chance of true)
  50. public static function boolean(chance:Number=0.5):Boolean {
  51. return (random() < chance);
  52. }
  53.  
  54. // sign(); // returns 1 or -1 (50% chance of 1)
  55. // sign(0.8); // returns 1 or -1 (80% chance of 1)
  56. public static function sign(chance:Number=0.5):int {
  57. return (random() < chance) ? 1 : -1;
  58. }
  59.  
  60. // bit(); // returns 1 or 0 (50% chance of 1)
  61. // bit(0.8); // returns 1 or 0 (80% chance of 1)
  62. public static function bit(chance:Number=0.5):int {
  63. return (random() < chance) ? 1 : 0;
  64. }
  65.  
  66. // integer(50); // returns an integer between 0-49 inclusive
  67. // integer(20,50); // returns an integer between 20-49 inclusive
  68. public static function integer(min:Number,max:Number=NaN):int {
  69. if (isNaN(max)) { max = min; min=0; }
  70. // Need to use floor instead of bit shift to work properly with negative values:
  71. return Math.floor(float(min,max));
  72. }
  73.  
  74.  
  75. // constants:
  76. // private properties:
  77. // public properties:
  78.  
  79. // constructor:
  80. public function Rnd(seed:uint=0) {
  81. throw(new Error("the Rnd class cannot be instantiated"));
  82. }
  83. }
  84. }

Report this snippet  

You need to login to post a comment.