GithubHelp home page GithubHelp logo

prashantkrsahu / biased-random-number-generator Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 5 KB

Write a custom random number generation algo which should be 73% biased to the higher number. Like if I want a random number between 1 to 10 100times then it should give number more than 5 73times and less than 5 27times.

Python 100.00%

biased-random-number-generator's Introduction

Biased-Random-Number-Generator

Write a custom random number generation algo which should be 73% biased to the higher number. Like if I want a random number between 1 to 10 100times then it should give number more than 5 73times and less than 5 27times.

Algorithm:

Step 1 : Take input from user:
a := lower limit
b := upper limit

Step 2 : Calculating mid value for biased probability
mid_val := (a+b)/2

Step 3 : Calculate range of biased numbers by mid_val (excluding mid_val) eg. 73% above 5 and 27% below 5
min_list := min_list will contain value from range 'a' to mid_val-1
max_list := max_list will contain value from range mid_val+1 to b+1

Step 4 : Calculate random number using Linear congruential generator(Pseudo-random Number Genrator)

	      while(length of max_list =! 73)	
		     number = (current_time +increment_value * multiplier) % mod
		     value = number/mod
		     value_in_range = (mid_val+1) + value * ((b+1)-(mid_val+1))
		     add value_in_range to max_list

	      while(length of min_list =! 27)	
	       number = (current_time +increment_value * multiplier) % mod
		     value = number/mod
		     value_in_range = a + value * ((mid_val-1)-a)
		     add value_in_range to min_list

Step 5 : Show min_list , max_list and final_list (concatenation of min_list and max_list)

Explanation:

  1. First take the lower limit and upper limit of range from user. Calculate mid value from these two limits to separate the range into two parts upper range and lower range.

  2. Then create two list say min_list which will contain values from lower limit to mid value(excluding) and max_list which will contain values from mid value (excluding) to upper limit.

  3. Start the while loop1 until the length of max_list becomes 73. In this loop random number is generated using Linear congruential generator(LCG) method in which an equation is used ie [number = (increment + current time * multiplier) % mod] and then 'number' generated from this equation is put into [value = number / mod] (which will give output value between 0 and 1). And this value is put into a formula [lower limit + value * (upper limit - lower limit] to calculate random number in given desired range. Then add final value to max_list

  4. Start the second while loop2 until the length of min list becomes 27. Same procedure is done as above while loop1 Then add final value to min_list

  5. Print both the max_list and min_list

  6. Then print final_list ie concatenated list of max_list and min_list

biased-random-number-generator's People

Contributors

aman9801 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.