random int / setting Propabiliity

In my program (A simple based lemonade stand game)
I want each "day" to have a non linear progression of temperatures.

But I don't want to make it completley random.

I dont want it to jump from 100 degrees to 50 to 55 to 99 to 65.. ya know?
I want it to be random for the next day but I want the temp from the previous day to be factored into the propability.

Is there a simple way of making a random integer pop up while also setting percetnages?

If the numbers are possible 1-9
I want it to randomly select an integer BUT

Sense it is "summer time" I want higher numbers to come up more often then lower ones. i thought about having it set thru if sttatements.

Such as randomly selecting 1-9

then IF x>3 (From this point 4-9 are all going to give it HIGH temp. while 1-3 are going to give it LOW temp. meaning its more likley to be high than low but... is there a more accurate (or simpler) way of doing this?

And how exactly would I code it out?
Assuming rand() returns an even distribution of random values (which I think there's some bias in some direction) then a long-winded way to do it would be to create an array (your "die") of a length greater than the range you want for a "roll" and set its values to the numbers you want back from the roll, except have some numbers duplicated, which increases the probability of getting that number. Then, get the number from the element of the array whose position you got from rand() % (size of the array);

-Albatross
rand() by itself is unbiased; it will generate every unsigned integer in the range [ 0... 2^32 - 1] (assuming 32-bit)
exactly once during its period.

The mod P operation introduces bias toward zero, if 2 ^ 32 mod P is not zero. For example, suppose that
rand() only returned integers in the range [ 0...9 ] unbiased. Then rand() % 4 is biased. Here is a table of
all possible outputs of rand() and rand() % 4:


rand() --> rand() % 4
0           0
1           1
2           2
3           3
4           0
5           1
6           2
7           3
8           0
9           1


0 and 1 occur with frequency 6/10.
2 and 3 occur with frequency 4/10.
Last edited on
Topic archived. No new replies allowed.