Parts Catalog Accessories Catalog How To Articles Tech Forums
Call Pelican Parts at 888-280-7799
Shopping Cart Cart | Project List | Order Status | Help



Go Back   PeachParts Mercedes-Benz Forum > General Discussions > Off-Topic Discussion

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 03-19-2010, 06:55 PM
TheDon's Avatar
Ghost of Diesels Past
 
Join Date: Oct 2005
Posts: 13,285
modulo operator?

ok how do you use this... I never learned this in math..ever.

I need to use it in programming to do something..


I have to take in 4 numbers into an array.. 4 "random" numbers between the values [0,5]...

so I have to "mod" the random numbers I think

that and I need to figure out why my array isnt taking in 4 values.

"

int i;
int number;
board[4];

srand(time(NULL));

for(i=0;i<4;i++)

board[i]=0;

for(i=0;i<5;i++)
{
board[number] = rand()%10;
}
"

Reply With Quote
  #2  
Old 03-19-2010, 07:09 PM
Kuan's Avatar
unband
 
Join Date: Jan 2001
Location: At the Birkebeiner
Posts: 3,841
A 12 hour clock uses Modulo-12 arithmetic. So 13 o'clock is 1 o'clock. 5=1 in Modulo-4 arithmetic.
__________________
You don't need a weatherman to know which way the wind blows - Robert A. Zimmerman
Reply With Quote
  #3  
Old 03-19-2010, 07:34 PM
Registered User
 
Join Date: Oct 2005
Posts: 4,263
Yes, you can use rand()%6 to get a number in the range of [0,5].

For any purists out there, I will note that as this uses only the low bits of the result of rand(), it is not very random. You should be using the high bits by dividing by RAND_MAX in floating-point, multiply by 6, then convert to an integer.

Also note that the modulus operator is well-defined on positive inputs only. That's not something to worry about with the return value of rand().
Reply With Quote
  #4  
Old 03-19-2010, 09:45 PM
TheDon's Avatar
Ghost of Diesels Past
 
Join Date: Oct 2005
Posts: 13,285
Quote:
Originally Posted by Matt L View Post
Yes, you can use rand()%6 to get a number in the range of [0,5].

For any purists out there, I will note that as this uses only the low bits of the result of rand(), it is not very random. You should be using the high bits by dividing by RAND_MAX in floating-point, multiply by 6, then convert to an integer.

Also note that the modulus operator is well-defined on positive inputs only. That's not something to worry about with the return value of rand().
I could do RAND_MAX deal ... could..
Reply With Quote
  #5  
Old 03-20-2010, 12:32 AM
Registered User
 
Join Date: Oct 2005
Posts: 4,263
Actually divide by RAND_MAX + 1.0. That way the quotient is less than one. Adding 1.0 rather than 1 promotes all values to double.

int random(int mod)
{
return (int)(mod * rand()/(RAND_MAX + 1.0));
}

random(N) will return an integer in the range of [0,N-1].
Reply With Quote
  #6  
Old 03-20-2010, 09:14 PM
TheDon's Avatar
Ghost of Diesels Past
 
Join Date: Oct 2005
Posts: 13,285
got it to generate 4 random numbers into the array

int i;
board[4];

srand(time(NULL));

for(i=0;i<4;i++)

board[i] = rand() %6 ;


now I need to do 7 more functions.. I think those functions need to call board[4] to compare against..

Reply With Quote
Reply

Bookmarks


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:20 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0
Copyright 2024 Pelican Parts, LLC - Posts may be archived for display on the Peach Parts or Pelican Parts Website -    DMCA Registered Agent Contact Page