New answers tagged random
2
A = Array[a, {7, 7}];
You can also use BlockRandom:
SeedRandom[123]
Drop[A, {BlockRandom[RandomInteger[{1, 7}]]}, {RandomInteger[{1, 7}]}] // MatrixForm
Alternatively,
SeedRandom[123]
Drop[A, {ri = RandomInteger[{1, 7}]}, {ri}] // MatrixForm
7
Perhaps either of these:
Drop[A, {#}, {#}]&@ RandomInteger[{1, Length@A}]
With[{i = RandomInteger[{1, Length@A}]}, Drop[A, {i}, {i}]]
2
Here are two similar methods of generating a pseudorandom list of unique vectors. The first method uses Union. It is a little faster than the second, which uses DeleteDuplicates. The two methods are implemented as functions f and g
Clear[f]
f[nvect_, ndims_, list_] := Module[{a = {}, k},
If[TrueQ[nvect <= Length[list]^ndims],
While[(k = Length[a]) ...
-1
First, there are $3^7=2187$ different 7-tuples, so there is indeed a considerable probability to encounter duplicates if we generate each 7-tuple independently. So, we use the following:
vectors=IntegerDigits[#, 3, 7] & /@ RandomSample[Range[3^7] - 1, 1000]
4
This is more of an extended comment. Here is a slight modification of your code:
parms = {m -> 1, ω -> 1, ℏ -> 1, α -> 1, n0 -> 8};
quadratures = ProbabilityDistribution[(1/Sum[1/k!, {k, 0, n0}]) *
Abs[Sum[(α E^(I ϕ))^n/√(n!) 1/Sqrt[2^n n!] ((m ω)/(π ℏ))^(1/4) *
Exp[-((m ω z^2)/(2 ℏ))] HermiteH[n, Sqrt[(m ω)/ℏ] z], {n, 0, n0}]]^2,
{z, -...
1
You should generate random pair once per each loop. E.g.
F = {x + y};
Do[If[F == # . {x, y}, Print[#]] &[
RandomInteger[{-1, 1}, {1, 2}]], 100]
or, in a more traditional way:
F = {x + y};
Do[pair = RandomInteger[{-1, 1}, {1, 2}];
If[F == pair . {x, y}, Print[pair]], 100]
2
While I think this question should be closed (because the main issue of efficiently generating samples without a loop is found in the documentation of RandomVariate), the following might help:
nSim = 10000;
n = 5;
SeedRandom[12345];
x = RandomVariate[NormalDistribution[0, 1], {nSim, n}];
cov = Covariance[#, 2 #] & /@ x;
Mean[cov]
(* 2.0006 *)
Histogram[...
4
Here's another option using Rasterize.
SeedRandom[1234];
img = Binarize[
Blur[ListDensityPlot[randomTiles, InterpolationOrder -> 0,
Frame -> False, PlotRangePadding -> 0,
ImageSize -> {1600, 1600}], 40]];
rimg = Rasterize[img, RasterSize -> 400, ImageSize -> 400]
ImageDimensions[rimg]
3
Use ColorFunction -> Graylevel in ListDensityPlot to start from a black and white image, avoiding Binarize altogether.
ask for a much higher number of pixels in your image with ImageSize; that alone causes considerable smoothing when you downsize the image;
for further effect, apply e.g. MedianFilter with an appropriate parameter.
SeedRandom[1234]
...
2
Since the input list not too large, we can also play with alternative approaches, among them RelationGraph:
SeedRandom[1]
length = 60;
inputlist = RandomSample[Range[0, 99], length]
{80, 14, 0, 67, 3, 65, 23, 68, 74, 15, 24, 4, 83, 70, 1, 30, 48, 25, 44, 73,
69, 56, 47, 28, 92, 26, 75, 10, 43, 33, 81, 18, 38, 29, 84, 17, 27, 85, 5, 40,
82, 22, 2, 39, 36, ...
1
First generate a random sample of integers between 0 and 99 (and this can be with or without replacement - but I've used "without replacement" in the example):
nOriginal = 60;
original = RandomSample[Range[0, 99], nOriginal]
Now find all of the available starting numbers (a) that have a+1, a+2, and a+3 available:
available = Select[original,
...
5
Here's a way that constructs a set of candidates by sorting the list and then breaking it into runs of consecutive elements and only keeping elements with enough successors to be viable. This is probably a good approach if you are going to be making multiple draws.
RandomSeed[1337];
test = RandomInteger[{0, 99}, 95]
(* {37, 84, 80, 98, 26, 32, 51, 65, 19, 33,...
3
Here is a procedural approach that does not require the list to be sorted.
I will first make some test data:
testlist = RandomSample[Range[0, 99], 50];
Then define a selector function:
testlist = RandomSample[Range[0, 99], 50];
ClearAll[selector]
selector[list_] :=
TimeConstrained[
Module[{choice},
While[!
ContainsAll[list, (choice = ...
2
To create some data for testing we get even numbers <100 and insert one odd number:
d = Table[i, {i, 0, 99, 2}];
d = Insert[d, 33, 18];
Now we can pick out a sequence of 3 integers in a row by:
SequenceCases[d, {x_, y_, z_} /; z == (y + 1) == x + 2, 1]
(* {{32, 33, 34}} *)
If you only want the first number, you would say:
SequenceCases[d, {x_, y_, z_} ...
0
Following an example, I tried to make it this way:
n = 100000;
SeedRandom[123456];
x = RandomVariate[CauchyDistribution[0, 1], n];
x = Sort[x, Greater];
meanExcess =Table[{x[[k + 1]], Mean[x[[1 ;; k]] - x[[k + 1]]]}, {k, 1, 99999}];
ListPlot[meanExcess, AxesLabel -> (Style[#, 18, Italic, Bold] &) /@ {"x[[k+1]]",
"meanExcess"}, ...
5
Three additional ways to implement empirical mean excess using
TruncatedDistribution + EmpiricalDistribution
UnitStep + Pick
Clip + DeleteCases
ClearAll[eme1, eme2, eme3]
eme1[data_, μ_] := Mean @ TruncatedDistribution[{μ, ∞}, EmpiricalDistribution @ data]
eme2[data_, μ_] := Mean @ Pick[data, UnitStep[data - μ], 1]
eme3[data_, μ_] := Mean @ ...
6
Spit-balling a little here, based on my best understanding of your definition:
n = 500;
variates = RandomVariate[CauchyDistribution[], n];
ClearAll[meanExcess]
meanExcess[data_, mu_] :=
Total[#]/Length[#]& @ Cases[data, x_?(# > mu &) :> x - mu]
Plot[meanExcess[variates, mu], {mu, 0, 10}]
Top 50 recent answers are included
Related Tags
random × 740probability-or-statistics × 124
distributions × 86
list-manipulation × 75
matrix × 53
plotting × 47
simulation × 47
performance-tuning × 39
graphics × 28
functions × 27
graphs-and-networks × 26
stochastic-calculus × 23
programming × 21
differential-equations × 19
function-construction × 19
graphics3d × 19
regions × 19
generative-art × 17
numerical-integration × 16
manipulate × 16
geometry × 16
algorithm × 16
equation-solving × 14
bugs × 14
version-7 × 14