How can I combine the output of a For loop into a list?

I am currently writing on a code to get RandomNumbers which are geometrical distributed.

But i need the output in one list because i want to plot the output Can anyone help me? Here is the current Code:

m := 20
k = RandomReal[{0, 1}, m];
n := 9
p := 0.40
Liste1 = CDF[GeometricDistribution[p], Range[0, n]];
For[
j = 1,
j < m + 1,
j++,
{
For[i = 1, i < n + 1, i++, If[k[[j]] < Liste1[[i]], Break[]]]; i - 1
}
Print[{i - 1, PDF[GeometricDistribution[p], i - 1]}]
]
• Use Table instead of For. (For is also very slow in Mathematica because, in constrast to C, there is no compiler that optimizes it away.) Moreover, Mathematica is not Maple or Pascal: The assignment operator is Equal (=), not SetDelayed (:=). The latter is used in combination with patterns for defining functions. – Henrik Schumacher Feb 9 at 10:34
• to get RandomNumbers which are geometrical distributed I might start with RandomVariate[GeometricDistribution[p]] and take it from there. – High Performance Mark Feb 9 at 10:41
• Why should I avoid the For loop in Mathematica? – corey979 Feb 9 at 11:22

To generate e.g. 1000 random numbers that are geometrically distributed with the probability parameter p=0.4

data= RandomVariate[GeometricDistribution[0.4], 1000]

To plot the data, use e.g. ListPlot or Histogram

As with other high-level functional languages and environments, Mathematica has an extensive library of functions. Initially it might be a bit overwhelming. But in your case, searching for "random number generation" will get you almost directly to the function RandomVariate

• I need to convert randomnumbers between 0 and 1 by my self with out any given function. Thats what my professor told me... – Mohamed Feb 9 at 12:49
• Perhaps this would help: math.stackexchange.com/questions/485448/… – FredrikD Feb 9 at 14:53

It is not clear what the restrictions are. You reject RandomVariate since you must do it by yourself "without any given function" -- but you use CDF and PDF. How about InverseCDF?

Clear["Global`*"]

p = 2/5;

n = 1000;

SeedRandom

sample = InverseCDF[GeometricDistribution[p], RandomReal[1, n]];

Show[
ListPlot[{#[], #[]/n} & /@ Tally[sample],
PlotStyle -> AbsolutePointSize],
DiscretePlot[PDF[GeometricDistribution[p], x], {x, 0, 14},
PlotStyle -> Directive[Red, Thick]]] 