June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,8 @@
10 DIM A$(9)
20 FOR I=0 TO 9 : READ A$(I) : NEXT
30 X = RND(-TI) : REM 'PLANT A RANDOM SEED'
40 X = INT(RND(1)*10)
50 PRINT A$(X)
60 END
100 DATA ALPHA, BRAVO, CHARLIE, DELTA, ECHO
110 DATA FOXTROT, GOLF, HOTEL, INDIA, JULIETT

View file

@ -3,4 +3,4 @@ import System.Random (randomRIO)
pick :: [a] -> IO a
pick xs = fmap (xs !!) $ randomRIO (0, length xs - 1)
x <- pick [1 2 3]
x <- pick [1, 2, 3]

View file

@ -1,5 +1,2 @@
import Data.Random
import Data.Random.Source.DevRandom
import Data.Random.Extras
x <- runRVar (choice [1 2 3]) DevRandom
sample $ randomElement [1, 2, 3]

View file

@ -1,3 +1,3 @@
import Test.QuickCheck (generate, elements)
x <- (generate . elements) [1, 2, 3]
do
x <- sample $ randomElement [1, 2, 3]
print x

View file

@ -1,13 +1,30 @@
// version 1.0.6
// version 1.2.10
import java.util.Random
/**
* Extension function on any list that will return a random element from index 0
* to the last index
*/
fun <E> List<E>.getRandomElement() = this[Random().nextInt(this.size)]
/**
* Extension function on any list that will return a list of unique random picks
* from the list. If the specified number of elements you want is larger than the
* number of elements in the list it returns null
*/
fun <E> List<E>.getRandomElements(numberOfElements: Int): List<E>? {
if (numberOfElements > this.size) {
return null
}
return this.shuffled().take(numberOfElements)
}
fun main(args: Array<String>) {
val list = listOf(1, 16, 3, 7, 17, 24, 34, 23, 11, 2)
println("The list consists of the following numbers:")
println(list)
val chosen = mutableSetOf<Int>()
println("\nFive elements chosen at random without duplication are:")
while (chosen.size < 5) {
val r = list[(Math.random() * list.size).toInt()]
if (chosen.add(r)) println(r)
}
println("The list consists of the following numbers:\n${list}")
// notice we can call our extension functions as if they were regular member functions of List
println("\nA randomly selected element from the list is ${list.getRandomElement()}")
println("\nA random sequence of 5 elements from the list is ${list.getRandomElements(5)}")
}

View file

@ -1,14 +1,4 @@
import math
randomize()
proc random[T](a: openarray[T]): T =
a[random(a.low..a.len)]
let ls = @["foo", "bar", "baz"]
echo ls.random()
var xs: array[10..14, string]
for i in 10..14:
xs[i] = "foo: " & $i
echo xs.random()
echo ls.rand()

View file

@ -1,3 +1,3 @@
(1..6).roll; # return 1 random value in the range 1 through 6
(1..6).roll(3); # return a list of 3 random values in the range 1 through 6
(1..6).roll(*); # return a lazy infinite list of random values in the range 1 through 6
say (1..6).roll; # return 1 random value in the range 1 through 6
say (1..6).roll(3); # return a list of 3 random values in the range 1 through 6
say (1..6).roll(*)[^100]; # return first 100 values from a lazy infinite list of random values in the range 1 through 6

View file

@ -1,5 +1,5 @@
# define the deck
my @deck = <2 3 4 5 6 7 8 9 J Q K A> X~ <♠ ♣ ♥ ♦>;
@deck.pick; # Pick a card
@deck.pick(5); # Draw 5
@deck.pick(*); # Get a shuffled deck
say @deck.pick; # Pick a card
say @deck.pick(5); # Draw 5
say @deck.pick(*); # Get a shuffled deck

View file

@ -0,0 +1 @@
>> random/only collect [repeat i 10 [keep i]]

View file

@ -1,6 +1,6 @@
aList = "abcdefghij"
for i = 1 to 10
letter = random(10)
letter = random(9) + 1
if letter > 0
see aList[letter] + nl
ok

View file

@ -1,4 +1,2 @@
irb(main):001:0> %w(north east south west).sample
=> "west"
irb(main):002:0> (1..100).to_a.sample(2)
=> [17, 79]
%w(north east south west).sample # => "west"
(1..100).to_a.sample(2) # => [17, 79]

View file

@ -1,6 +1,10 @@
extern crate rand;
use rand::Rng;
fn main() {
let array = [5,1,2,5,6,7,8,1,2,4,5];
println!("{}", rand::thread_rng().choose(&array).unwrap());
let mut rng = rand::thread_rng();
println!("{}", rng.choose(&array).unwrap());
}

View file

@ -0,0 +1,10 @@
Option Explicit
Sub Main_Pick_Random_Element()
Debug.Print Pick_Random_Element(Array(1, 2, 3, 4, 5, #11/24/2017#, "azerty"))
End Sub
Function Pick_Random_Element(myArray)
Randomize Timer
Pick_Random_Element = myArray(Int((Rnd * (UBound(myArray) - LBound(myArray) + 1) + LBound(myArray))))
End Function