Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to generate randomly incrementing number over time

Tags:

random

math

I'm trying to make a fake download count. It should increment randomly over time. Some download-count-like patterns would be nice.

Is this possible without using a database, or storing a counter anywhere?

My idea is to check the number of seconds that have passed since my app was released. Then just throw that into a formula which spits out the fake download count. Users can request to see the download count at any time.

Is there a math function that increments randomly? I could just pass my secondsPassed into there and scale it how I'd like.

Something like this: getDownloadCount(secondsPassed)

enter image description here

Edit: here's an example solution. But it gets worse performance over time.

downloadCount = 0
loop secondsPassed/60 times // Loop one more time for every minute passed
  downloadCount += seededRandom(0, 10)
like image 501
Farzher Avatar asked May 14 '15 17:05

Farzher


People also ask

Which function is used to generate random number?

Matlabs random number generation function is called rand. In Matlab, the rand function returns a floating point number between 0 and 1 (e.g., . 01, . 884, .

How do you get the program to generate a random number between 1 and 10?

Java Random number between 1 and 10 Below is the code showing how to generate a random number between 1 and 10 inclusive. Random random = new Random(); int rand = 0; while (true){ rand = random. nextInt(11); if(rand != 0) break; } System.


1 Answers

Making a fake download count doesn't sound like a nice thing to do. However in designing secure communication protocols, there are legitimate use cases for monotonically growing functions with some randomness in their values.

I am assuming you have:

  • A growth model given as a monotonically growing function providing approximate values for the desired function.
  • Access to a time stamp, which never decreases.
  • Ability to store a constant random seed along with the function definition.
  • No way to store any updated data upon the function being queried.

First you decide on a window length, which will control how much randomness will be in the final output. I expect you will want this to be on the order of one hour or a few.

Figure out which window the current time is within. Evaluate the reference function at the start and end of this window. Consider the rectangle given by start and end time of the window as well as min and maximum value given by the reference function. Feed the corners of this rectangle and your constant seed into a PRNG. Use the PRNG to choose a random point within the rectangle. This point will be on the final curve.

Perform the same computation for one of the neighbor windows. Which neighbor window to use depend on whether the first computed point on the curve is to the left or the right of the current time.

Now that you have two points on the curve (which are reproducible and consistent), you will have to iterate the following procedure.

You are given two points on the final curve. Consider the rectangle given by those corners. Feed the corners and your constant seed into a PRNG. Use that PRNG to chose a random point within the rectangle. This point will be on the final curve. Discard one of the outer points, which is no longer needed.

Since the Y-values are restricted to integers, this procedure will eventually terminate once your two points on the curve have identical Y-coordinate, and you will know, that the function has to be constant between those two points.

like image 96
kasperd Avatar answered Sep 21 '22 14:09

kasperd