Page 1 of 1

A Way to call random values involving minimum value:

PostPosted: Fri Mar 29, 2013 3:06 am
by bat78
Hi,
I was making something, but i noticed that i can't randomize with minimum. So i used that:
Code: Select all
setpen(min(100,100)+rand(255), 0, 0, 0, 1);

It seems, work, the pen's color is random from 100 to 255. If someone is interested, can tell me is that the right way and if it exist another one.
I have to be sure c:
..Since i can randomize with minimum, i can make something to blink with rgb vars:

Or i can do something like: That should make an canvas blink with two colors.
Code: Select all
int color
color = rand(255)
setpen(color, 0, 0, 0, 1);
if (color > 99) {
color = 255;
}

if (color < 100) {
color = 0;
}

Re: A Way to call random values involving minimum value:

PostPosted: Fri Mar 29, 2013 4:37 am
by skydereign
Your use of min isn't needed. min returns the smaller of the two values you pass it, so if you pass it the same value, it will just return it. But in general you are doing the right thing.
Code: Select all
int var = 100+rand(155); // will return a random value between 100 and 255
// so 100 is the minimum value

Notice you add the minimum value, because rand returns a value from 0 to the number you pass it. You can make a function for this if you want.

Re: A Way to call random values involving minimum value:

PostPosted: Fri Mar 29, 2013 9:25 am
by bat78
Ohh true :oops: i didn't toticed that min(a, b=a)+rand(c) will return just the number a, like i declare it simply a+rand(c).. Just tough that min is expection for rand, because it returns max and if there is a max, min sounds a part of that too. However i couldn't understand tho that:
int var = 100+rand(155); // will return a random value between 100 and 255 It returns from 100 fine, but why since it is declared 155, it will return max of 255

Re: A Way to call random values involving minimum value:

PostPosted: Fri Mar 29, 2013 9:36 am
by skydereign
The rand function returns a random number between zero and the number you pass it. If you remove the rand from that line, you get the minimum value. But, that rand(155) will return a random number within the range 0-155. Assume it returns 0, then we get the minimum value (100 + 0). And if it returns 155, we get 255 as the max (100 + 155). Remember, you are adding the rand value to the minimum value.

Re: A Way to call random values involving minimum value:

PostPosted: Fri Mar 29, 2013 9:52 am
by bat78
Its not like that if i define the minimum by min function :o