need to add odds of value getting picked inside random

I have a code much like this

ran1 = random(1,100);
ran2 = random(100,1000);

delay(random(ran1,ran2));

That means its a 1 out of 2 odd for either ran1 or ran2. Now I have a VERY ugly and wrong way of changing the odds. So ugly that I'm ashamed of posting it here. So how would I make it say... 1 in 20 chance of getting ran2?

Warning! I have not googled this yet so if you reply within the next 4 hours you are encouraging extreme laziness. ]:smiley:

Generate a random number in a range of 0 .. 19 and test whether it is a specific value. On average it will be, one time in twenty.

I was thinking it would be something like that but I'm not sure what command line I would use.

I think your answer lies in this:

and test whether it is a specific value

(Hint: any specific value)

Yeah I noticed. But I don't think using an if else command is the right way to handle this and thats the only way I can think of to check it. I know it sounds right. I'll post my code later so you can see why I think it might be wrong. Is there another line to use other then if else?

I don't really see why you need an else clause.
Either do something if it is your one-in-twenty event, or do nothing.

ran1 = random(1,100);
ran2 = random(100,1000);

ran1 is a random number from 1 to 100 - so it could be 23.
ran2 is a random number from 100 to 1000 - may be 792.

delay(random(ran1,ran2));

Now you delay by a random number of milliseconds from 23 to 792. So, perhaps 298 - or 25 or 681 or ....

That means its a 1 out of 2 odd for either ran1 or ran2

How do you get 1 out of 2 odds?

Pete

Its a 50/50 chance that it would pick ran2 for the value. I need it to be a 30/70 chance.

mcreefer:
Its a 50/50 chance that it would pick ran2 for the value. I need it to be a 30/70 chance.

It's not a 50/50 chance, it will generate a random number between ran1 and ran2. Sounds like your looking for an if else statement based on a random something like if(random(0,99) < 30).

mcreefer:
delay(random(ran1,ran2));That means its a 1 out of 2 odd for either ran1 or ran2.

Based on this quote, and others following, it looks like you expect this code to delay for either ran1 or ran2 milliseconds. That's not what it will do. It will select a random number between ran1 and ran2, and delay for that number of milliseconds. The probability density functions of these two calculations are quite different.

Brush up on how random() works here: random() - Arduino Reference.

This might give you what you want:

delay(random(k)>=x?ran1:ran2);

You'll have to select constants k and x to get the probabilities that you expect - either 1/20, or 70/30, depending on which of your posts I read. There's no direct "if" statement, but the "?:" construction is the moral equivalent. If you select 1 of n items completely at random, you have some other options, but if you want to explicitly define the probabilities, it looks like you're stuck with some kind of conditional.

I have this, which I think should work. I have yet to test.

ran1 = random(1,21);
if(ran1 < 20)
{
  low = random(1,100);
}
else
{
  low = random(100,1000);
}