how to rand between three integer num1 num2 num 3

Recommended Answers

All 8 Replies

use the library(header file) #include<stdlib.h>
then use this function : srand(i)%3+1
where i is your variable.

commented: Learn to use the function before telling someone how to wrongly use it. -3

for example we have obj1 obj2 obj3
can you right for me the random formula ?

post your working

You want a random selection. There are a few ways, but storing those items in a container for random indexing is by far the simplest:

#include <array>
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main()
{
    srand((unsigned)time(nullptr));

    array<int, 3> a = {11, 22, 33};

    cout<<"Random element: "<< a.at(rand() % a.size()) <<'\n';
}

nop it's not what i mean

let say that:
num=num2/3
num1=num3/5

how to make random between num and num1

My C is bit rusty but in pseudo code
(max_of_random - min_of__random)/(wanted_max-wanted_min)*random_value +wanted_min

You keep changing what you ask for. Now answering:

let say that:
num=num2/3
num1=num3/5

how to make random between num and num1

Assuming num < num1: random-number = (rand() % (num1 - num)) + num

>>how to rand between three integer num1 num2 num 3

You say in between the three integers. That means one of the numbers, either num1, num2 or num3 has to be a min or the max. Which means the last one has to fall in between. Thus this implies that you want a number in the range of , [ min(num1,num2,num3) to max(num1,num2,num3) ]; So a general formula is rand() % (max - min ) + min , or min + (max-min) * (rand()/(float(RAND_MAX+1))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.