Pages: [1]   Go Down
Author Topic: Numbers + Random  (Read 376 times)
0 Members and 1 Guest are viewing this topic.
South Louisiana
Offline Offline
Full Member
***
Karma: 0
Posts: 178
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So.... i have seen quite a few programs on here that do a wide variety of things in the last month or so i have been playing with the arduino and scavenging all the information i can get about kewl projects i may be interesting in putting together.

One thing i can't see the difference in is people who do the number add method instead of just typing the number.

for example, whats the difference in...

random (255);

or

random ((120) + 135);

Does it really matter? i have seen quite a few people who do this addition method but normally it seems it would be easier just to put what you want in there and continue on......i dunno maybe i'm just thick.
 
:-/ i'm confused.....
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46243
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When the random function actually get invoked, the result is the same value being passed in.

The reason for not adding the numbers together in advance generally comes form the separate numbers having meaning to the author, but the resulting added number looses that significance.

For example, if I'm writing code to simulate the roll of dice, I'd be inclined to write random(6 + 6); than to write random(12); because it's more obvious (to me) that 6 has significance whereas 12 requires a moment's thought.
Logged

Waterloo, Canada
Offline Offline
Full Member
***
Karma: 1
Posts: 242
Engineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Bad example.
To simulate the roll of the dice you'd have to write random(6) + random(6).

(Sorry, couldn't resist...)
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46243
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Of course, you're right. It was the only thing that I could think of right away. There are cases, though, where adding the numbers together obscures the meaning, and letting the Arduino add them takes very little time.
Logged

South Louisiana
Offline Offline
Full Member
***
Karma: 0
Posts: 178
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok. So pretty much it's for no programming significance other than to make the code easier to read if the numbers have alternate meaning to the viewer.

Thanks.....
Logged

Pages: [1]   Go Up
Jump to: