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]];
 j = 1,
 j < m + 1,
   For[i = 1, i < n + 1, i++, If[k[[j]] < Liste1[[i]], Break[]]]; i - 1
  Print[{i - 1, PDF[GeometricDistribution[p], i - 1]}]
  • 2
    $\begingroup$ 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. $\endgroup$ – Henrik Schumacher Feb 9 '19 at 10:34
  • 2
    $\begingroup$ to get RandomNumbers which are geometrical distributed I might start with RandomVariate[GeometricDistribution[p]] and take it from there. $\endgroup$ – High Performance Mark Feb 9 '19 at 10:41
  • 2
    $\begingroup$ Why should I avoid the For loop in Mathematica? $\endgroup$ – corey979 Feb 9 '19 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

| improve this answer | |
  • $\begingroup$ I need to convert randomnumbers between 0 and 1 by my self with out any given function. Thats what my professor told me... $\endgroup$ – Mohamed Feb 9 '19 at 12:49
  • $\begingroup$ Perhaps this would help: math.stackexchange.com/questions/485448/… $\endgroup$ – FredrikD Feb 9 '19 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?


p = 2/5;

n = 1000;


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

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

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.