hello forum

I need help in making a function that randomly chooses between two numbers that I give through the parameters. Please give me a hint or something. Thanks in advance guys.

Recommended Answers

All 12 Replies

What have you tried so far?

so far nothing except googling the function generating random numbers from a defined area. e.g. any number from 1 to 30. Haven't found anything significant yet.

Are you looking to choose a number within that range or are you looking to choose between one of the two numbers given?

i'm trying to make it choose only one of the two given numbers. In particular i want it to chose either 1 or -1. I want to repeat the process for 2 and -2

So have your routine choose either 0 or 1 (so use rand() % 2) and then select the appropriate number with an if statement. I'm sure there are plenty of other (and better) ways to do it but that's off the top of my head.

So have your routine choose either 0 or 1 (so use rand() % 2) and then select the appropriate number with an if statement. I'm sure there are plenty of other (and better) ways to do it but that's off the top of my head.

I tried it your way but the results are the same every time. What I'm looking for is a function that chooses either 1 or -1 and then either 2 or -2 and that's it

Yes, you need to

#include <time.h>
and then one time before you call rand() (so e.g., at the top of main) call
srand((unsigned)time(0));

which seeds the random number generator

Yes, you need to

#include <time.h>
and then one time before you call rand() (so e.g., at the top of main) call
srand((unsigned)time(0));

which seeds the random number generator

Thanks
I'll try that

>So have your routine choose either 0 or 1 (so use rand() % 2)
>and then select the appropriate number with an if statement.
rand() % 2 is especially risky when it comes to getting the same result every time (for reasons I'm not interested in detailing at the moment). This has worked better for me in the past:

if (rand() < RAND_MAX / 2)
{
  /* Pick one number */
}
else
{
  /* Pick the other number */
}

rand() % 2 is especially risky when it comes to getting the same result every time (for reasons I'm not interested in detailing at the moment).

Good to know and noted.

if you want to get ranom number between 1 and 2 you can not do this

num = rand() % 2

you must instead do this

num = rand() % 2 + 1

because otherwise will never get the number 2 and will lots of times get 0.

i hope that this will help you, and that you are feeling healthy! God Bless!! :) :) :)

commented: Try reading the post that come before. This suggestion was already shown to be inadequate. -2

if you want to get ranom number between 1 and 2 you can not do this

num = rand() % 2

you must instead do this

num = rand() % 2 + 1

because otherwise will never get the number 2 and will lots of times get 0.

The OP doesn't want a random number between 1 and 2, he wants to randomly choose between two options. It doesn't matter what two random numbers you use for the comparison, or even if you compare against a range. The only thing that matters is that there are ultimately two randomized results.

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.