# Increasing probability with a number

Hi,
This is my first post so you can tell I'm a noob.
I have an idea of a project which takes a number read from a file and then outputs.

However this is where I'm stuck, I want this number to increase the probability of outputting as it gets larger.
Although I'll only have one output so it is either on or off.

The number won't be larger than 100.
My idea was to divide the number by 10 and round up or down. Then generate a number between 1 and 10 and compare it to the divided number.

This is kinda hard to explain so an example.
The number is 34
Divided by 10 = 3.4 (rounded down to 3)
Then it will generate a number and if it is greater than 3 it will be off and if its less than or equal to, it would activate the output.

Is this feasible? And is there any better way to do it?
Thanks,

Why bother dividing by 10 when you could just compare 34 to 30. Using integers is a lot faster on an Arduino.

I think you need to give us a few examples that illustrate how the size of the number affects things.

...R

An Internet search for something like "generating random numbers with arbitrary distribution" would get you started. e.g. Randomly Generating Numbers with an Arbitrary Distribution

Your first problem is describing the arbitrary distribution you desire, but once that is solved, there are well defined algorithms to do so.

What do you mean by "probability"? You will need to define that clearly before you can write a computer program.

For example, the weatherman defines the probability of rain as follows:

if the chance of rain is 10% today, on about 10 days out of 100 just like this one, it will rain and on about 90 it will not.

Robin2:
Why bother dividing by 10 when you could just compare 34 to 30. Using integers is a lot faster on an Arduino.

I think you need to give us a few examples that illustrate how the size of the number affects things.

...R

That's a great point, I was doing that just to make it simpler (Was constructing the idea late night/early morning) but maybe its not and I might be able to get a more precise chance that way.

An example of how the size affects it would be, if the number was 80, there is an 80% to activate the output and only 20% not to. But if it was 30, there would be a 30% chance for it to activate and 70% not to.

``````bool chance(int threshold, int maximum)
{
return random(maximum) < threshold;
}
``````

fill in for maximum 100 to get percentage

``````if (chance(80, 100) )
{
// will be executed 80% of time
}
else
{
// will be executed 20% of the time
}
``````

Gregulimy:
An example of how the size affects it would be, if the number was 80, there is an 80% to activate the output and only 20% not to. But if it was 30, there would be a 30% chance for it to activate and 70% not to.

Maybe my brain is asleep tonight but I can’t relate that to your earlier example

The number is 34
Divided by 10 = 3.4 (rounded down to 3)
Then it will generate a number and if it is greater than 3 it will be off and if its less than or equal to, it would activate the output.

which does not seem to have any probability attached to it. If it is greater than 3 it will be off

…R

Gregulimy:
The number is 34
Divided by 10 = 3.4 (rounded down to 3)
Then it will generate a number and if it is greater than 3 it will be off and if its less than or equal to, it would activate the output.

To place probability on it, the generated number has a 70% chance of being greater than 3 and a 30% chance of being less than or equal to 3 (So in theory a 70% chance of being of being off, and 30% of being on)

Sorry for the confusion (Starting to confuse myself again)

I've done something like this:

``````x = random(2);    //  The number is 0 or 1.   There is a 50% chance the number is zero.

x = random(3);    //  The number will be between  0 and 2.   It will equal zero 1 out of 3 times.

x = random(10);  //  There's a 10% chance the number will be zero.
``````

I've used that method for 50/50 quite a bit, and in my applications I've used one-out-of-3 quite a bit too.

But, that won't work if you are reading numbers from a file...

Another method that I haven't used is to generate a random number between 1 and 100.

Once you have a random number between 1 and 100, there is a 50% chance it's 50 or less. There's a 1% chance it's one, and a 99% chance it's less than 100, etc.

That method will only work (with the expected probabilities) if the numbers read from your file are truly random.

And... Probability and randomness means there is uncertainty... If you flip a coin 100 times, you expect to get heads 50 times. But, there's a good chance of getting a different result. If you run that 100-flip experiment many times the average should be about 50 heads. You can flip a coin 5 times and get heads 5 times in a row... It's just unlikely.

DVDdoug:
Another method that I haven't used is to generate a random number between 1 and 100.

Once you have a random number between 1 and 100, there is a 50% chance it's 50 or less. There's a 1% chance it's one, and a 99% chance it's less than 100, etc.

That method will only work (with the expected probabilities) if the numbers read from your file are truly random.

Yeah that makes sense.
The numbers will be under 100 and to two decimal places most likely, but then rounded

Gregulimy:
To place probability on it, the generated number has a 70% chance of being greater than 3 and a 30% chance of being less than or equal to 3 (So in theory a 70% chance of being of being off, and 30% of being on)

I wonder if what you are trying to say is that you want to generate a random number and then compare it to see if it is higher or lower than a control value?

That should be very straightforward.

...R

Robin2:
That should be very straightforward.

See post #5

There is a 90% chance OP has not tried that code yet

robtillaart:
There is a 90% chance OP has not tried that code yet

I have the impression from your many contributions that you know a bit of maths. Hence you very properly subtracted 10% and got 90%

I would have added and got 110% ...R

There is a 90% chance OP has not tried that code yet

My guess would have been 99.9%

It is much easier to discuss problems on the forum than to actually solve them.

jremington:
It is much easier to discuss problems on the forum than to actually solve them.

That is definitely true, I kinda halted the project for time being.
But thankyou all for your help
(and I will try that code)