#### Archived

This topic is now archived and is closed to further replies.

# some kinda of mathmatical engine?

This topic is 5393 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I need to create some kind of a engine that will choose random colors and random numbers at same combination. and vise versa. The colors woudl be like green, yellow, red, purpule, orange and 1 black. si this possible? if so how? rich

##### Share on other sites
Most programming languages have some built-in random number generation function. You should probably try using that, or explaining what you are trying to do with somewhat more detail.

##### Share on other sites
this is what I am trying to do..

There are 6 dice.Each dice is six sided and each side of the dice their is a color. Blue, Green, Black, Yellow and white. Their no numbers, only colors. I need to make a engine or some kind of function that will randomly choose colors. There are six dice total. Can you help me out?

Rich

##### Share on other sites
Just use a random number generator. Check up a tutorial or reference on random numbers for whatever language you''re using.

##### Share on other sites
kingvandal,

The advice given by eighty and Geoff is good. I will give you all the details you need to know how to use the random number generator to solve your problem.

You should use a random number generator. Use that to generate a random integer, then map the random integer value to a color. So here is a way you can set up your map in a simple function, in C++. I assume you have a Color object, but you should be able to figure out how to adapt this to your code.

Color GetColor(unsigned int uiColorCode){  switch(uiColorCode)  {    case 0:      return(Color::Blue);    case 1:      return(Color::Green);    case 2:      return(Color::Black);    case 3:      return(Color::Yellow);    case 4:      return(Color::White);  }  return(Color::White); // default}

The rand() method (or similar) available in most modern computer languages will generate a random integer been 0 and some maximum. For C/C++ the maximum value happens to be RAND_MAX. So, suppose you have a maximum color index of 4. You can then map the result of rand() to be between 0 and 4:

#define MAX_COLOR_CODE 4
uiColorCode = (rand() * MAX_COLOR_CODE) / RAND_MAX;

Send that to GetColor to get your random color.

Make sense?

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

##### Share on other sites
I would recommend using:

#define MAX_COLOR 5 // this is max color +1 really

rand()%MAX_COLOR;

% is a modulo operator.... ie the remainder when divided by MAX_COLOR... in effect capping the value. This avoids the * and / (though they would probably be converted to a specific value by the compiler, so it won''t make a difference probably).

##### Share on other sites
Ok about now you guy are going to start flaming me. I am not a programmer. I am a computer analyst/network admin asst. I am creating a small simple game for my uncle. I want the game to roll dice and as it rolls to generate the colors in random form. ( As I read on a site, they are not really random ). I am have the images that he wants in the game. But as for the programming part I am a fool. so nay indepth how to help is apperiated.

Rich

##### Share on other sites
quote:
Original post by dmounty
I would recommend using:

#define MAX_COLOR 5 // this is max color +1 really

rand()%MAX_COLOR;

% is a modulo operator.... ie the remainder when divided by MAX_COLOR... in effect capping the value. This avoids the * and / (though they would probably be converted to a specific value by the compiler, so it won''t make a difference probably).

YES, absolutely. This is better. Thank you for the recommendation!

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

##### Share on other sites
quote:
Original post by kingvandal
Ok about now you guy are going to start flaming me. I am not a programmer. I am a computer analyst/network admin asst. I am creating a small simple game for my uncle. I want the game to roll dice and as it rolls to generate the colors in random form. ( As I read on a site, they are not really random ). I am have the images that he wants in the game. But as for the programming part I am a fool. so nay indepth how to help is apperiated.

Rich,

It is okay that you aren't a programmer, and hopefully nobody will flame your for that. But, we won't actually write your game for you, and we (in this forum) won't tell you how to build your game. If you want to create the game then you will need to actually write some code on your own.

From the point-of-view of math and physics, the above posts answer every question you've asked here. We've already given you indepth help on the mathematics and logic of your color selection, which is what you asked for. The original suggestions to use the built-in random number generator were appropriate. And my code, combined with dmounty's (use his function to calculate uiColorCode instead of mine) are C language code that you can just copy and paste and it will probably work. You'd have to change the logic for the GetColor function to deal with the manner in which you want to represent color, but otherwise we have provided working C language source code that you are free to use.

Now, if you have bigger questions, for example "how to I draw the dice on the screen" or "how to I create an application for Windows or Macintosh," then you will have to ask different questions in a different forum. This forum focuses only on math and physics. More general programming questions, including general game programming questions, belong elsewhere. For example:

Gamedev Game Programming Forum
Gamedev General Programming Forum

or, even, for beginning game developers and nonprogrammers:

Gamedev For Beginners Forum

So, I hope you don't take this message as being harsh. I just want you to understand that from our point of view we have already answered your question. But others on gamedev.net can help with any other questions you may have.

And I do, sincerely, hope that you are able to create your small game, .

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

[edited by - grhodes_at_work on October 15, 2003 2:33:48 PM]

##### Share on other sites
I think this would be a good spot to use an enumaration.

enum DICECOLORTYPE
{
GREEN = 0, YELLOW, RED, PURPLE, ORANGE, BLACK
};

int RollDice()
{
return(random(6));
}

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002286
×