1

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]}]
 ]
Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309
Mohamed
  • 11
  • 1

2 Answers2

2

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

FredrikD
  • 1,868
  • 1
  • 13
  • 25
  • 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 09 '19 at 12:49
  • Perhaps this would help: https://math.stackexchange.com/questions/485448/prove-the-way-to-generate-geometrically-distributed-random-numbers – FredrikD Feb 09 '19 at 14:53
1

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[0]

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

Show[
 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

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198