Skip to content

Instantly share code, notes, and snippets.

@autarc
Forked from 140bytes/LICENSE.txt
Created November 6, 2011 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save autarc/1342386 to your computer and use it in GitHub Desktop.
Save autarc/1342386 to your computer and use it in GitHub Desktop.
Creating a Random Number

Random - 112bytes

A "random()" function which can probably be useful from time to time. It requires at least one entry as a boundary and returns a random integer between it and the second one (~ 0 if you used just one entry).

Improvements

There is still one thing that bugs me: even though you are able to use any positive and negative number as an input, it currently just calculates the result correctly if none of them got floating points. I know there would be the possibility to check if one contains some (=> c = (a%1!=0)||(b%1!=0) ), but unfortunately I ran out of space. Since there seem to be the potential saving some bytes, e.g. like the condition for the second parameter check, there is hopefully the chance to enhance it. It would be great if you have an advice to shorten the code or know or nice way of determining the amount of floats :)

Updates

  • thanks @tsaniel: pointing to keeping the scope, not counting the function name and regarding parantheses (140->112)
/* Normal */
------------
function random(a,b,c){
c=a;
a=(!b)?0:((a<=b)?a:b);
(!b)?((a>c)?(b=a):(b=c)):((b>c)?(b=b):(b=c));
a=(a^b)?a:c;
return(a+(0|Math.random()*(b-a+1)))
}
/* Commented */
---------------
function random(
a, // boundary one (integer)
b, // boundary two (integer)
c, // placeholder
){
c=a; // temporary cache, which helps ordering the entries (saves "a")
a=(!b)?0:( // checks if the function got none second parameter
(a<=b)?a:b); // @true: 0 will be stored
// @false: checks if the second parameter is larger/equal
then the current one
(!b)?( // checks if the function got none second parameter
(a>c)?(b=a):(b=c)):( // @true: either a or c will be stored (the larger one)
(b>c)?(b=b):(b=c)); // @false: b will be compared to c, if its larger it will be kept
a=(a^b)?a:c; // reassigning the value to a (allows usage of negative numbers)
return(a+(0|Math.random()*(b-a+1))) // returns the random number, using "0|" instead "Math.floor()"
}
/* Current */
function(a,b,c){c=a;a=!b?0:a<=b?a:b;!b?a>c?b=a:b=c:b>c?b=b:b=c;a=a^b?a:c;return a+(0|Math.random()*(b-a+1))};
/* version #1 */
function random(a,b,c){c=a;a=(!b)?0:((a<=b)?a:b);(!b)?((a>c)?(b=a):(b=c)):((b>c)?(b=b):(b=c));a=(a^b)?a:c;return(a+(0|Math.random()*(b-a+1)))}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "randomNumber",
"description": "Creating a random number (incl. negatives & shorthands).",
"keywords": [
"random",
"number",
"useful"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>undefined</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
/* showing some random numbers */
var random = function(a,b,c){c=a;a=!b?0:a<=b?a:b;!b?a>c?b=a:b=c:b>c?b=b:b=c;a=a^b?a:c;return a+(0|Math.random()*(b-a+1))};
document.write('A random Number between:' + '<p>');
document.write('<li>0 and 255: '+ '<u>' + random(255) + '</u></li>');
document.write('<li>10 and 100: '+ '<u>' + random(10,100) + '</u></li>');
document.write('<li>-50 and 50: '+ '<u>' + random(-50,50) + '</u></li>');
document.write('<li>42 and -23: '+ '<u>' + random(42,-23) + '</li>');
</script>
@tsaniel
Copy link

tsaniel commented Nov 6, 2011

I just glanced your code, and there are something you should know.

  • we don't count the function name as bytes
  • remove necessary parentheses
  • the scope of variables

@autarc
Copy link
Author

autarc commented Nov 6, 2011

@tsaniel Nice,thanks for the advices. Didn't knew how "beautiful" minimalistic js can be without parentheses. A part of me still things that the chained logic needs to brick, but I did a few tests and it still works like a charm Oo

With the new 28 bytes left, there hopefully be a good chance to enhance the floating point issue :)

@xpansive
Copy link

xpansive commented Nov 7, 2011

A little reordering saves two bytes:
return 0|Math.random()*(b-a+1)+a

Edit: Whoops, that appears to be one off for negative numbers...

@tsaniel
Copy link

tsaniel commented Nov 7, 2011

What about

function(a,b){with(Math)return(random()*(max(a,b|=0)-(a=min(a,b))+1)|0)+a}

?

@autarc
Copy link
Author

autarc commented Nov 7, 2011

@xpansive: Yeah unfortunately it doesn't work for them :(

@tsaniel: Wow, I definately still have a lot to learn about js. Didn't knew the methods Math.max() and Math.min() or even the way to call it before "with(Math)" to avoid repetition. It even deals with floating points - just not in the way I thought. Using "11.5" as a value returns values from 0 - 12, instead of 0.0 to 11.5. If you don't mind - I will use your code further to dig into this (^_^) (112->74)

@tsaniel
Copy link

tsaniel commented Nov 7, 2011

Feel free to use - would like to see more your work ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment