Finding all possible outcomes of two dice using Outer in Mathematica - wolfram-mathematica

I am just getting started with Wolfram Mathematica 9, and am trying to learn how to use the Outer function. I have a problem where I want to generate a list containing all possible outcomes from rolling two dice. I have the following list to start with.
numbers = { 1, 2, 3, 4, 5, 6 }
The list I want to generate should be on the following form.
{ { 1, 1 }, { 1, 2 }, { 1, 3 }, ..., { 6, 6 } }
I have tried things like
Outer[Times, numbers, numbers]
to no avail. How do I solve this problem?
Thank you in advance!

If you want to stick to Outer try Outer[List,numbers,numbers].

As alluded to, there is another way to do this. Consider
Subsets[Range[1, 6], {2}]

You can also use Join by Outer[{#1}~Join~{#2},numbers,numbers].

Related

Non-mutating sorted list in Dart

I can sort a list like this in Dart:
final myList = [6, 3, 7, 1, 0, 2];
myList.sort();
However, this is a destructive sort since it mutates it in place. I'd like to do something like this:
final myList = [6, 3, 7, 1, 0, 2];
final newList = myList.sorted();
where my list stays the same but newList contains the sorted list. Dart apparently doesn't have this functionality, so how do I implement that myself?
In searching for the answer to this question my main struggle was knowing the proper way to copy a list, which wasn't as obvious as it seems like it should be. I found the answer to that, so I am also posting an answer to my original question below, Q&A style.
You can create a new sorted list without affecting the original list like so:
final myList = [6, 3, 7, 1, 0, 2];
final sorted = myList.toList()..sort();
Calling toList() copies the list. The .. is to get a reference to the list itself since sort() is a void function. This gives the same result:
final sorted = myList.toList();
sorted.sort();
Printing the values of both lists gives the following results:
print(myList); // [6, 3, 7, 1, 0, 2]
print(sorted); // [0, 1, 2, 3, 6, 7]
You can read more about copying lists here.
I'd suggest you to create a fixed-length list.
final myList = [6, 3, 7, 1, 0, 2];
final sortedList = myList.toList(growable: false)..sort();
It seems to me that this question is really "How can I copy a List?", and the sorting aspect is tangential.
There are multiple ways to copy an Iterable to a new List:
Using a method on the original Iterable: iterable.toList()
Using a List constructor: List.of(iterable)
Using the spread operator in a List literal: [...iterable]. Note that this always creates a growable List.
Note that List.from should not be used since that uses dynamic types and loses type information. Effective Dart prefers .toList().
Also note that the above applies to Sets too (iterable.toSet(), {...iterable}, Set.of(iterable)).

Java Stream Collectors sort the given stream [duplicate]

This question already has answers here:
How is this HashSet producing sorted output?
(5 answers)
Closed 2 years ago.
can you please let me know why below code returns the set sorted order given an unsorted array?
Stream<Integer> s = Stream.of(2, 3, 1, 4, 5);
Set<Integer> mySet = s.collect(Collectors.toSet());
System.out.println(mySet);
O/p
1, 2, 3, 4, 5
This doesn't happen if I use List instead of Set.
Also the sorting is not always correct when there are negative numbers in the input.
Is there any inbuilt functionality to sort the Set?
This is just an accident and has nothing to do with Collector or Stream. On OpenJDK 11.0.2, I'm getting this (as Collector.toSet() is currently backed by HashSet):
Set<Integer> set = new HashSet<>();
set.add(2);
set.add(3);
set.add(1);
set.add(4);
set.add(5);
System.out.println(set); // [1, 2, 3, 4, 5]
But try this:
set.add(100);
System.out.println(set); // [1, 2, 3, 4, 100, 5]
It is really just an accident. A very curious one, indeed, which has to do with the Integer.hashCode() implementation (it's just the Integer.intValue() itself), and how this results in accidentally ordering HashSet elements for certain sets.
It is just a coincidence. The Set interface does not provide any ordering guarantees, but LinkedHashSet.

Dice Rolling Probability in using R studio

So I want to know how to set this up to get the correct answer in R.
If you roll two standard 6-sided die, estimate the probability that the difference between them is 3, 4, or 5 (and not 0, 1, or 2).
I know how to set up the basic model of finding a sum but am not sure on how I can find the difference. PLEASE HELP!!!
I don't know exactly what you are doing. I guess you simulate the die rolls? So you have a data.frame with two columns, each containing the rolls of one die?
Like this:
die_rolls <- data.frame(die1 = c(1, 2, 6, 3, 1, 4), die2 = c(3, 5, 6, 2, 1, 1))
Then you could create a new column that calculates the difference. And then uses the absolute value of it, i.e. you won't have negative values.
die_rolls$difference <- abs(die_rolls$die1 - die_rolls$die2)

Genetic algorithm, cross over without duplicate data

I'm creating a genetic algorithm and I just encounter a problem, let's take an example. I have a list of numbers : [2, 3, 6, 8, 9, 1, 4] which represent my datas.
The best solution to my problem depends on the order of the numbers in the list. So I have two solution : S1 [2, 3, 9, 8, 1, 6, 4] and S2 [1, 6, 4, 3, 9, 2, 8]
If I do a basic cross-over with S1 and S2 I may obtain a solution like this : child [2, 3, 9, 8, 9, 2, 8] and we can see that the solution is bad because I duplicate datas.
The question is how may I realized an evolution (so cross-over) without duplicate thoses datas ?
thanks.
You will need a crossover operator like Ordered Crossover (OX1) that can perform crossover without duplicate thoses datas:
OX1:
A randomly selected portion of one parent is mapped to a portion
of the other parent. From the replaced portion on, the rest is filled
up by the remaining genes, where already present genes are omitted and
the order is preserved.
You should take care with mutation too, because it can change the genes order, in this case you can use a mutation operator like Reverse Sequence Mutation (RSM).
In the reverse sequence mutation operator, we take a sequence S
limited by two positions i and j randomly chosen, such that i<j.
The gene order in this sequence will be reversed by the same way as
what has been covered in the previous operation.
You have Permutation Encoding, look at this explanation: http://www.obitko.com/tutorials/genetic-algorithms/crossover-mutation.php
In general you take the elements of the first parent in order in which they are met in the first parent and you take the rest of the elements in the order in which they are met in the second parent.

Ruby extracting array values

I want to extract certain values from one array and concat them into another empty one:
freqs=[1,12,4,15,7,8,11,5,6]
less_freqs=[]
This is what I've come up with.
freqs.collect{|x| x<9 then x.to_a{|y|less_freqs<<y}}
Perhaps a different method? And, I'm not even sure if then makes any sense.
Is this what you're looking for?
freqs = [1,12,4,15,7,8,11,5,6]
less_freqs = freqs.select{|x| x < 9 } # => [1, 4, 7, 8, 5, 6]

Resources