6
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by firstPerson
-1

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

Votes + Comments
Learn to use the function before telling someone how to wrongly use it.
0

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

0

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';
}
0

nop it's not what i mean

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

how to make random between num and num1

0

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

Edited by pyTony: n/a

0

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.