Hello All
Tell me who is correct I or my friend

void main()
{
    int GuessMe[4]={100, 50, 200, 20};
    int Taker=random(2)+2;
    for(int Chance=0;Chance<Taker;Chance++)
        cout<<GuessMe[Chance]<<"#";
}

I am testing random() in Turbo C++ 3.5
By this code I am always getting answer 100#50# whenever I am executing this code.
My argument is random() always returning 0 from 0 & 1 so 0+2 is 2

but My friend challanged me with his logic
int Taker = random(2) + 2;
random(2) returns either 0 or 1
Hence, Taker is either (2+0=2) or (2+1=3)

IF taker is 3, then as

for(int Chance=0; Chance<Taker; Chance++), Chance can go from {0,1,2}!!

Hence, the output can be 100#50#200#
My question is if my friend's logic is correct then why random() is not returning 2?

What is random? It's not a standard function. Did you define it yourself or does it come with Turbo C++?

Anyway this random function probably needs to be seeded just like the standard rand function does. So if you don't provide a seed, you'll get the same sequence of random numbers each time when you run your program.

@sepp2k random is a function f Tubo C++ which is in header file stdlib.h see the help screen shot
1eac1024674c55c0c2352c68d456dd9f
Seed is 2

I'm not sure that the seed is really 2 - you don't seem to set a seed anywhere in your code (the docs you showed, don't mention how to set the seed for random, so I assume you'd set it using srand just like you would when using the rand function).

But if it really is 2, that's your problem right there: If the seed is a constant, the numbers you get will be the same each time. You need a changing seed (like the current system time) to get different numbers.

@sepp2k it is what you are seeing. Even if I change the number within random() from 2 to any other number is showing same result means random is returning 0 always. How can prove my friend's logic is not proper

My question is if my friend's logic is correct then why random() is not returning 2?

Did you read the info in your screenshot?
It states that random(num) will return a number from 0 to num-1
So if num=2(your code) random will return 0 or 1.
If num=3 random will return 0,1 or 2.

Here is the documentation for the random function. It appears that random(N) generates a number between 0 and N-1, very much like the more standard rand() % N would do. The randomize() function is the equivalent of srand((unsigned int)time(NULL)), which is used to seed the random number generator.

So, to the OP's question, it appears that your friend's logic is correct, i.e., the code random(2) + 2 should output either 2 or 3.

It goes without saying that these functions are not recommended at all. First, they are not standard functions (look at rand() / srand() for standard C functions, and look at the <random> header for the standard C++ options). Second, these functions are only available in Turbo C, which is more than 20 years old, and pre-dates even the first official version of C++, and is barely more recent than the first ISO-standard version of C. And third, these random number generators are poorly implemented (mod-range is non-uniform, and seeding with time is not great).

Comments
+1

@mike_2000_17 If my friend logic is correct then why I am getting always 0 from random(2), why not getting 1 atleast once out of 37 execution of the code.

Edited 2 Years Ago by vishalonne

If my friend logic is correct then why I am getting always 0 from random(2), why not getting 1 atleast once out of 37 execution of the code.

You never seed the random number generator with a call to randomize(), therefore you will always get exactly the same result with your calling random(2).

Edited 2 Years Ago by nullptr

@nullptr then why my friend is getting 1 from random(2) with same code (without randomize()). What is this difference between us by same code.

Edited 2 Years Ago by vishalonne

This article has been dead for over six months. Start a new discussion instead.