Latest 100 public
snipts » random
showing 1-20 of 24 snipts for random
-
∞ Generate a random numeric zero-padded string
/** * Function to generate a random numeric zero-padded string * @param int $minChar Minimum character of the numer * @param int $maxChar Maximum character of the numer **/ function getRandom($minChar=5, $maxChar=9){ $min=pow(10,$minChar-1); $max=pow(10,$maxChar)-1; list($usec, $sec) = explode(' ', microtime()); srand((float) $sec + ((float) $usec * 100000)); return str_pad(rand($min,$max),$maxChar,"0",STR_PAD_LEFT); }#end getRandom
-
∞ Random redirect with Javascript
<script type="text/javascript"> var urls = new Array("http://usabilla.com/rate/A", "usabilla.com/rate/B"); window.location = urls[Math.floor(urls.length*Math.random())]; </script>
-
∞ Real Random Sang Kaghaz Gheichi
#include <iostream> #include <conio.h> #include <time.h> #include <stdio.h> #include <stdlib.h> using namespace std; int main() { loop: time_t t; srand((unsigned) time(&t)); int cpu=rand(); cpu%=3; cpu++; cout<<"\a\nSang-Kaghaz-Gheichi? (1,2,3)--0 to exit--- "; int user; cin>>user; if(!user) return 0; cout<<"\nCPU choosed "<<cpu<<"."; if(cpu==user) cout<<"\nYou equalized!"; else { switch(user) { case 1: if(cpu==2) cout<<"\nCPU Won!"; else cout<<"\nYou Won!"; break; case 2: if(cpu==3) cout<<"\nCPU Won!"; else cout<<"\nYou Won!"; break; case 3: if(cpu==1) cout<<"\nCPU Won!"; else cout<<"\nYou Won!"; break; default: cout<<"\nWhat was that? Please enter 1-2-3!!"; } } getch(); cout<<"\n\n"; goto loop; }
-
∞ Print a random element in an Array
#!/usr/bin/perl use strict; # Declare an Array my @coins = ("Quarter","Dime","Nickel"); # Print a random element print $coins[rand @coins];
-
∞ Debugged
#include <iostream> #include <string> using namespace std; void whyCallMe(string input) { if (input == "urgent") cout << "because it is urgent lar" << endl; else if (input == "miss you") cout << "you wait long long"<<endl; else cout << "Not answering your calls"; } void plsDunCallMe() { string a = "there is more then one way to skin the cat"; cout << a << endl; } int func1(int x, int y) { int result = 0; result = x + y; return result; } int main() { int result = 0,x=10,y=20; string strIn = ""; cout << "Spot the common mistake(s)" << endl; result = func1(x,y); cout << "sum 10+20: " << result << endl; plsDunCallMe(); while (1) { cout << "Why You Called?"; getline(cin,strIn); whyCallMe(strIn); cout << endl; if (strIn == "urgent"); } return 0; }
-
∞ Very interesting way to generate a random string. Doesn't work on Snow Leopard tho.
</dev/urandom tr -dc A-Za-z0-9./%? | head -c 10
-
∞ randRange - random number within a given range
private function randRange(start:Number, end:Number):Number{ return Math.floor(start +(Math.random() * (end - start))); }
-
∞ random md5
md5(uniqid(rand(), true)); -
∞ Load random mp3's to mp3 player
#!usr/bin/perl -w # http://ubuntuforums.org/attachment.php?attachmentid=17255&d=1160475724 # http://ubuntuforums.org/showthread.php?t=216102 ###################################################################################### # Author : Jan Roald Haugland janroald@gmail.com # ###################################################################################### # No effort is made to make this a good script :-) # # Free to use, free to modify # # If something goes to hell; dont blame me. No guarantees provided... # ###################################################################################### ###################################################################################### # Syntax : # # perl copyrandom.pl [source directory] [destination] ([max megabytes to transfer])# # Example : # # perl copyrandom.pl /home/you/mp3/ /media/ipod/ 500 # ###################################################################################### #500 is MB's you want to transfer (maximum). This is also default if u dont specify # ###################################################################################### ###################################################################################### # Config ############################################################################# # Add or remove extesions to be transferred here, separate with comma $extensions_allowed = 'mp3,ogg,wma'; # Edit number of megabytes to be transferred if not specified as argument $default_mb = 500; # Only edit below if u know what u are doing ###################################################################################### ###################################################################################### # Parameters ######################################################################### $source = $ARGV[0]; if($source !~ m/\/$/) { $source = $source.'/'; } $destination = $ARGV[1]; $max_mb = (!$ARGV[2]?$default_mb:$ARGV[2]); $extensions_allowed =~ s/,/|/g; # Other variables #################################################################### my $mb_total = 0; #Start counting my $file = ''; my @list = (); my @files = (); my %files_test = (); my $dir = ''; ###################################################################################### print "\n--- Looking for songs in $source ... ---\n\n"; print "--- Copying maximum ${max_mb}MB's to $destination... ---\n\n"; # Using shell 'ls' to list all files and directories @list = `ls -1pshR $source`; splice(@list, 0, 1); # Looping through the list, selecting directories and songs # Generating songlist with trailing megabytes $x = 0; foreach(@list) { if($_ =~ /^\.\/(.*):$/) { $dir = "$1/"; $dir =~ s/([\s\(\)!&'"`\[\],\|:])/\\$1/g; } if($_ =~ /^$source(.*):$/) { $dir = "$1/"; $dir =~ s/([\s\(\)!&'"`\[\],\|:])/\\$1/g; } if( $_ =~ /^\s?([\d|\.]*)M\s((.*)\.($extensions_allowed))$/i ) { $file = $2; $mb = $1; $file =~ s/([\s\(\)!&'"`\[\],\|:])/\\$1/g; push(@files, "$dir$file|$mb"); } $x++; } # Picking out random files and copying to destination. Making same song isn't copied twice $number_of_files = @files; while($max_mb > $mb_total) { $random = rand($number_of_files); ($song,$size) = split('\|',$files[$random]); if(!$files_test{$song}) { $mb_total+=$size; $files_test{$song} = 1; if($max_mb > $mb_total) { `cp $source$song $destination`; print "### Transferred file $song ### ${size}MB's ### ${mb_total}MB's total ###\n"; } } } -
∞ Random alphabetical and numerical strings
<?php function randomString($max) { $alfabet = "abcdefghijklmnopqrstuvwxyz"; $paraula = ""; for($i=0;$i<$max;$i++) { $paraula .= $alfabet[floor(rand(0,25))]; } return $paraula; } function randomInt($max) { $numeros = "1234567890"; $paraula = ""; for($i=0;$i<$max;$i++) { $paraula .= $numeros[floor(rand(0,9))]; } } ?>
-
∞ Random Name Generator
#include <iostream> #include <ctime> #include <string> using namespace std; int main() { srand(time(0)); char gName; string firstname[] = {"John", "Tim", "Andy", "Ronald", "Aaron", "Michael", "Tony", "Hardy", "Harry", "Ron"}; string lastname[] = {"Roberts", "Kennedy", "Alonso", "Gates", "Norman", "Fields", "Potter", "Carter", "Jackson", "Thomas"}; do { cout << "Random Name Generated: " << firstname[rand() % 10] << " " << lastname[rand() % 10] << endl; cout << "Generate another name?(Y/N) "; cin >> gName; cout << endl; }while((gName == 'Y') || (gName == 'y')); return 0; }
-
∞ random number as3
var ran:int = Math.round(Math.random() * (high - low)) + low;
-
∞ RandWall
#!/bin/bash # # randwall # # Muda de papel de parede em OpenBox e derivados. # Com base em um diretório pré-determinado ou com # base em um parametro passado pelo usuário. if [ "$1" ]; then WALLPAPERS=$1 else WALLPAPERS=$HOME/img/wallpapers fi if [ ! -d $WALLPAPERS ]; then echo "Invalid directory." exit 0 fi LISTA=( `ls -w1 "$WALLPAPERS"` ) FAIXA=${#LISTA[*]} MOSTRA=$(( $RANDOM % $FAIXA )) if [ -f $WALLPAPERS/${LISTA[$MOSTRA]} ]; then nitrogen --set-centered "$WALLPAPERS/${LISTA[$MOSTRA]}" else echo "$WALLPAPERS/${LISTA[$MOSTRA]} is not a valid file." fi
-
∞ javascript random string function
function rnd(){ return String((new Date()).getTime()).replace(/\D/gi,'') }
-
∞ Random string
<?php $chars = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9)); $len = 16; $output = ''; for ($x = 0; $x < $len; $x++) $output .= $chars[array_rand($chars)]; echo $output; ?>
-
∞ Simple generation of random string in Python
import string import random print "".join(random.sample(string.letters+string.digits, 8))
-
∞ Text Munger: flips all the letters but the first and last into a random order.
<?php error_reporting(0); function shuffleAd ($word) { $firstLetter = substr ($word, 0, 1); $word = substr_replace($word, "", 0, 1); $lastLetter = substr ($word, (strlen($word)-1), 1); $word = str_shuffle(substr_replace($word, "", (strlen($word)-1), 1)); return "$firstLetter$word$lastLetter "; } if ($_POST["submit"]) { $text = $_POST['text']; echo "<form id=\"form\" name=\"form\" action=\"munger2.php\" method=\"post\"><input type=\"text\" id=\"text\" name=\"text\" /><input type=\"submit\" id=\"submit\" value=\"Add!\" name=\"submit\" /></form>"; $array1 = explode(" ", $text); //put string into an array, divided by spaces $arraySize = count($array1); for ($i = 0 ; $i < $arraySize ; $i++) //for each word I type print stripslashes(shuffleAd($array1[$i])); } else { echo "<form id=\"form\" name=\"form\" action=\"munger2.php\" method=\"post\"><input type=\"text\" id=\"text\" name=\"text\" /><input type=\"submit\" id=\"submit\" value=\"Spin Around!\" name=\"submit\" /></form>"; } ?>
-
∞ Lua random thing
str = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890" for i = 0,#str do i = i + 1 io.write(string.sub(str,i,i)) end
-
∞ Simulador de moneda
/* Programa que simula lanzamiento de moneda // Yamil Gonzales // Programación I */ #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; int flip(); int main () { int cara, cruz, tiraLaMoneda; for (int i=0; i < 100; i++) { tiraLaMoneda = flip(); if (tiraLaMoneda == 1) { cout<<"Salio cara"<<endl; cara++; } else { cout<<"Salio cruz"<<endl; cruz++; } } cout<<endl<<endl<<"Total cara: "<<cara<<endl <<"Total cruz: "<<cruz; system ("pause"); return 0; } int flip() { int caraCruz; int random = rand(); int parOno = random % 2; random = rand(); parOno = random % 2; if (parOno == 1) { caraCruz = 1; } else { caraCruz = 0; } return caraCruz; }
-
∞ flexible random password generator
<?php function generate_password($length, $type) { $alpha = "abcedefghijklmnopqrstuvwxyz"; $bigalpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $numeric = "0123456789"; $symbol = "!@#$%^&*-+_"; switch ($type) { case 'alphasmall': $possible = $alpha; break; case 'alpha': $possible = $alpha . $bigalpha; break; case 'alphanumsmall': $possible = $alpha . $numeric; break; case 'alphanum': $possible = $alpha . $bigalpha . $numeric; break; case 'all': $possible = $alpha . $bigalpha . $numeric . $symbol; break; } $password = ''; for ($i=0;$i<$length;$i++) { $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); $password .= $char; } return $password; } ?>


