Recent

Author Topic: Help needed for an algorithm  (Read 839 times)

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Help needed for an algorithm
« on: July 22, 2019, 08:14:42 pm »
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!
« Last Edit: July 22, 2019, 08:17:00 pm by minesadorada »
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Help needed for an algorithm
« Reply #1 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.
« Last Edit: July 22, 2019, 08:40:46 pm by Thaddy »
Specialize a type, not a var.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Help needed for an algorithm
« Reply #2 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

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Re: Help needed for an algorithm
« Reply #3 on: July 22, 2019, 10:56:55 pm »
Thanks folks - I knew I could rely on this forum
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

 

TinyPortal © 2005-2018