# Random of min max values

## Recommended Posts

Howdy all, is it possable of making two integers defined by user, A minimum and maximum integers, and generating a random number between the fields of minimum and maximum. i am not sure if its a math question or logic, or combo of both... A given exemple : Main.cpp
#include "Main.h"
#include "Math.h"
int main() {
RandMinMax(7,14);
return 0;
}


Math.h
#ifndef MATH_H
#define MATH_H

#include <ctime>
#include <stdlib.h>
#include <iostream>
int RandMinMax(unsigned int min, unsigned int max) {
srand(time(0)); int GenerateNumber = rand(); // These commands declere and generate the random numberic.
int GetDiffrence = (max - min);
if(min < 0 || max < 0) { printf("value cant be set to a nagative number"); return 0; }
else if(min > max) { printf("Minimum cant be bigger then maximum"); return 0; }
else { int Damage = ...("what to do here?"); printf("%d Damage string", GetDamage); return 1; }
return 0; }
#endif



##### Share on other sites
number = (rand() % (max - min)) + min;

that'll work with negatives too

-me

##### Share on other sites
Wow Thanks, so simple :) suprised havent figured that out as well...

##### Share on other sites
I would just add, that you did something in your posted code alot of beginners mistakenly do, you put srand(time(0)); in your random number function. You should put that outside that function and call it only once, generally when the program is first started. Otherwise the algorithm Paladine posted works fine for the most part. One can be nitpicky and say it is inherently flawed since it is using rand() which is generally accepted as being a poor rng.

##### Share on other sites
Quote:
 number = (rand() % (max - min)) + min;

Note that the maximum is not inclusive. Using your numbers 7 and 14, we get:

rand()%7+7;

rand()%7 will generate numbers from 0-6, adding 7 to the range gets you 7-13. You also don't need the parenthesis around the rand because modulus has higher precedence. So if you want inclusive max bounds:

number = rand()%(max - min + 1) + min;

rand() just returns a pseudorandom number from 0 to RAND_MAX (32767). Your using modulus to bring the number into a desired range. Modulus just gives the remainder of the division.

-Dev578

##### Share on other sites
And using modulo does not give you an even distribution. As long as RAND_MAX is large and your range is small, it's not as big of an issue. Modulo won't give an even range unless the range divides evenly into RAND_MAX.

##### Share on other sites
Since you guys were a great help to me , i would take my risk by asking another question related to randomize numbers and events...

What am i doing wrong here?
assuming the enum is been converted to a single numberic such as
FIRE = 0;
FROST = 1;
and so on...
however the code seems to take the random number its spose to generate and turn it into the actual number...
E.G
int type = rand()%3 + 1; (= 3 (poision) instead of generating one of the 3 events shown below he choose the third event...

enum Elements {FIRE, FROST, POISION, LIGHTNING};void CauseDMG::SpectralHit() {	int type = rand()%3 + 1;	bool loop = true;	while(loop == true)	{	switch(type) {	case FIRE:		{ dmg+=3; printf("+3 Fire damage given"); }		loop = false;		break;	case FROST: // Frost damage gives an extra Rule turn...		{ dmg+=5; printf("+5 Frost damage given"); }		loop = true;		break;	case POISION:		{ dmg+=6; printf("+6 poision damage given");}		loop = false;		break;	case LIGHTNING:		{ dmg+=7; printf("+3 Lightning damage given");}		loop = false;		break;	default:		{ dmg+=4; printf("+4 Physical damage given");}		loop = false;		break;		}	} };

##### Share on other sites
You are probably calling srand every loop with the same seed value (most likely 0)

1. You random number generation will only give values 1, 2 or 3. It can never be 0 so a FIRE attack will never happen
2. There is no point to the while loop if you are constaining the random number between the possible values of the enum (which you are).

##### Share on other sites
Quote:
 Original post by yaustarYou are probably calling srand every loop with the same seed value.Other problems in your code:1. You random number generation will only give values 1, 2 or 3. It can never be 0 so a FIRE attack will never happen2. There is no point to the while loop if you are constaining the random number between the possible values of the enum (which you are).

1. I noticed that :)
2. As soon as i will be able to fix the random event.
The while loop will take the correct effect.

For starter, i am mad at myself for making a stupid mastike such as this

" int type = rand()%3 +1; " // ITS not 1 - 4 events
should be int type = rand()%4; // 0 - 4

second of all you were right, reuse of srand cause that problem so thanks :)

##### Share on other sites
Then try this
enum Elements {	FIRE = 0, 	FROST, 	POISION, 	LIGHTNING,	ELEMENTS_TOTAL};int randomAttack = rand() % ELEMENTS_TOTAL;

I added an extra enum value to Elements: ELEMENTS_TOTAL. Since FIRE is 0, the value of ELEMENTS_TOTAL will automatically be the number of elements in the enum (in this case 4). You can now use this constant to ensure you never get a random value more then the number of enum values in Elements. This also means that you can add more elements without changing other sections of code that rely on ELEMENTS_TOTAL. As long as it is at the end, the value will always be the number of values in the enum.

Personally, this is how I would have written it:
namespace Elements{enum EElements {	FIRE = 0, 	FROST, 	POISION, 	LIGHTNING,	TOTAL_NUMBER};}Elements::EElements randomAttack = static_cast<Elements::EElements>( rand() % Elements::TOTAL_NUMBER );`

The namespace encapsulates all the enum values and randomAttack is the same type as the enum that it will be checked against for type safety.

##### Share on other sites
Think ill cover the entire random area :)
Hope this is the last question i have in that topic...

how do you randomize your event with percentages?
for a certain event, Event #1 will have 90 percent for been triggered
and Event #2 will have 10 percent for been triggered...?

##### Share on other sites
You're starting to get into the concept of probability distributions. What you've been mucking around in is the uniform distribution where every event has an equal chance of occuring.

I think the easiest way is to define each event having a Range of values. If you have two events where one event is 90% and the other 10%, then you can get a random number between 0 and 9 and then the numbers between 0 and 8 are event one, and 9 is event 2. This can be generalized for larger numbers of events.

Of course, if you use modulo then your distribution will in fact be different because, say, rand() % 10 is NOT evenly distributed. That is, some numbers will occur more often than others. This will end up skewing the ranges given above.

Hope this helps.

edit: I should probably mention solutions around using % for getting your random numbers instead of just saying it will cause problems. This site seems pretty good at explaining techniques to get a better distribution.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628367
• Total Posts
2982279

• 10
• 9
• 13
• 24
• 11