# Generate random numbers with certain spikes? [duplicate]

I am trying to generate random numbers to simulate steering wheel angles and velocities of a car. So for steering wheels the random numbers could go like 1,2,4,6,4,3,40,0. I can generate random numbers but the problem is it wouldnt make sense to have truly random numbers for steering angle data. So usually a driver only turns a few degrees unless there is a turn and somtimes he will not steer and the steering angle would be just zero. Can someone give me some advice regarding this?

## marked as duplicate by gnat, Arseni Mourzenko, GlenH7♦, jmoreno, Kilian FothMar 3 '15 at 10:14

• This problem is too vague. Are you trying to choose from a subset of integers with equal probability or are you trying to choose from all integers with non-equal probability? In other words, are {0, 1, 2, 4, 6, 3, 40} the only choices, or do you simply want some numbers to turn up more often than others? – Doval Feb 25 '15 at 14:45
• These are not the only choices. I want to simulate steering angles. So a driver usually changes the steering angle little by little he doesnt make crazy steers like 0,40,70,20. – zzzzz Feb 25 '15 at 14:48
• Are the numbers evenly distributed in time? Will the driver ever accelerate or decelerate (or stop)? Is he driving in a closed track or an infinite stretch of road? – Doval Feb 25 '15 at 14:55
• This question is already answered on Stack Overflow: Weighted random numbers. That question is for C++ but the algorithm in the accepted answer should apply to any language. Also see: Generating weighted random numbers. – user22815 Feb 25 '15 at 15:29

As you've noted, random steering wheel angles don't make sense.

For example:

Assume the wheel is straight. You can randomly apply deltas (perhaps in the range of +/- 2 degrees) to the wheel position to simulate guiding the vehicle down a straight road.

You can introduce a bias to those deltas (say, +/- 2 degrees, plus another 0 - 5 degrees) to introduce a bend in the road.

You'll want to keep track of the absolute position of the steering wheel, especially if you're adding sharp corners (it's not possible to turn the wheel +90 degrees 20 times in a row).

You could create a list of discrete steering behaviors where each one has a set of wheel angles that are executed in succession:

• Right Turn
• Sharp left turn
• avoid pot hole or other obstacle
• veer off road (someone was texting and driving)
• change lane

Then you can randomly select through this list. Each item could be weighted to occur more often depending on the type of driving you'd like to simulate: off-road, highway, inner city, country road, etc.

• and how do I generate the number for each item? – zzzzz Feb 25 '15 at 15:06
• The number for the steering wheel degrees? – JeffO Feb 25 '15 at 17:41

You can map the values to a function that rises sharply at one end and then select from this.

I think this function might work well:

``````y = 1/(1.1 - x)
``````

If x is your randomly produced number and y is the value you use for steering.

Most of the time your values will be fairly small, but occasionally the output of the function will rise to very high value giving you the peaks you want.

Tweaking values of this function will allow you to get different results such as higher peaks or less likely peaks.

• I think this is the best answer if a non-discrete distribution is needed. For a discrete distribution, this Stack Overflow question would be ideal. – user22815 Feb 25 '15 at 16:35