Random of min max values

Started by
10 comments, last by nobodynews 16 years, 11 months ago
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




Thanks in advance.
Advertisement
number = (rand() % (max - min)) + min;

that'll work with negatives too

-me
Wow Thanks, so simple :) suprised havent figured that out as well...
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.
.
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
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.
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 it`s 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;		}	} };
You are probably calling srand every loop with the same seed value (most likely 0)

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 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).

Steven Yau
[Blog] [Portfolio]

Quote:Original post by yaustar
You 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 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).



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 :)
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.

Steven Yau
[Blog] [Portfolio]

This topic is closed to new replies.

Advertisement