A Way to call random values involving minimum value:

Talk about making games.

A Way to call random values involving minimum value:

Postby bat78 » Fri Mar 29, 2013 3:06 am

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;
}
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

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

Postby skydereign » Fri Mar 29, 2013 4:37 am

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

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

Postby bat78 » Fri Mar 29, 2013 9:25 am

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
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

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

Postby skydereign » Fri Mar 29, 2013 9:36 am

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

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

Postby bat78 » Fri Mar 29, 2013 9:52 am

Its not like that if i define the minimum by min function :o
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest