# Lazarus

## Programming => General => Topic started by: minesadorada on July 22, 2019, 08:14:42 pm

Title: Help needed for an algorithm
I want to generate 1000 random numbers with a configurable bias.  It is for an app I am planning to write (for fun) - the generator will be a class of course.

• Scenario 1: 50% numbers over 0.5. - Easy - just use Random()
• Scenario 2: x% numbers over 0.5 - a general algorithm
There are some awesome programmers on this forum that can help me I am sure.  I can't come up with efficient code for scenario(2) on my own.
I promise all that this is not homework - I left school 50 years ago!
Title: Re: Help needed for an algorithm
Post by: Thaddy on July 22, 2019, 08:39:03 pm
Has already been done for us:
https://wiki.freepascal.org/Generating_Random_Numbers#Exponential_Distribution
(and others, with slightly differing implementations).
I think for your purpose this is a good match but read the wiki entry carefully: there are more gems there, like Erlang.
Title: Re: Help needed for an algorithm
Post by: winni on July 22, 2019, 08:43:09 pm
With 2 loops this is very easy:

Code: Pascal  [Select][+][-]
1. // x percent over 0.5
2. for i := 1 to x do rand := random * 0.5 +0.5;
3. // 100 -x percent below 0.5
4. for i := 1 to 100 -x do rand := random * 0.5;
5.

Store the values in an array of single and your done.

Not very elegant but it works.

Winni
Title: Re: Help needed for an algorithm