Hello
I have a > b(for example)
I need to generate random number x that belongs from a to b
for example a = 9, b = 16
x has to be x > 9 and x < 16
I use cstdlib.. but can't guess how to do it (using rand())
can you do it, without any while or for??
this is a part of a huge program...
so I have to do it carefully, this my program doesn't work..
I think the reason is that...
this is a part of my program ...
the excercise was about a game
in that game one guy think of a number,and can say hotter or colder, and the second one tries to guess it very fast...
this code is the part of second that have to call Guess(G) very few times
but analyzing the mind of player wasn't easy
for example first's number was 12(and he thought about number between 1 and 20)
and it can said 5 11 15, or 5 9 11, or 18 4 16 or 11 15 13
I got to write a code that have to know what number should be next
analyzing temperature of each number....
in both cases hot or cold
I thought about debugging this program, but I still don't know how to GDB it(??)
to see where it goes, and what are my variables' values at the each line...
this is my nightmare
even if you can't solve it, let's discuss it
command work is always the best

#include<cstdlib>
#include"grader.h"
#include<iostream>
#include<ctime>
using namespace std;

int HC(unsigned int N)
{
	unsigned short int n = 0;
	unsigned short int G;
	unsigned short int one_past_of_G=1;
	unsigned short int previous_of_past_of_G =1;
	short int temper_of_previous = 0;
	short int temper_of_pastpast = 0;
	register int i;
	int test;
	G = (rand() % N)+1;
	short int temper = Guess(G);
	temper++;
	one_past_of_G = G;
	srand(time(NULL));
	G = (rand() % N)+1;
	
	while(temper != 0 || Guess(G) != 0){
		n++;
		temper = Guess(G);
		if(n > 2){
			if(temper == 1){
				if(previous_of_past_of_G < one_past_of_G && one_past_of_G < G){
					if(temper_of_previous == 1){
						if(temper_of_pastpast == 1){
							for(i = 0; test <= G+5 && test > one_past_of_G || i < 10; i++){
								test = ((rand()%one_past_of_G-(one_past_of_G))+(G +1));
								if(test > one_past_of_G && test < G+5){
									G = test;
								}
							}
						}else{
							G = ((rand()%previous_of_past_of_G-(previous_of_past_of_G))+(G +1));
						}
					}
									cout << "(temper == 1){\n" <<
									"\tif(previous_of_past_of_G < one_past_of_G && one_past_of_G < G) \n" << G;
				}
				else if(previous_of_past_of_G > one_past_of_G && one_past_of_G < G){
					if(temper_of_previous == -1){
						if(temper_of_pastpast == 1){
							if(G <= previous_of_past_of_G){
								G = ((rand()%G-(G))+(previous_of_past_of_G+1));
							}else{
								G = ((rand()%previous_of_past_of_G-(previous_of_past_of_G))+(G+1));
							}
						}
					}
					cout << "(temper == 1){\n" <<
									"\tprevious_of_past_of_G > one_past_of_G && one_past_of_G < G \n" << G;
				}else if(previous_of_past_of_G < one_past_of_G && one_past_of_G > G){
					if(temper_of_previous == -1){
						if(temper_of_pastpast == 1){
							if(G <= previous_of_past_of_G){
								G = ((rand()%G-(G))+(one_past_of_G+1));
							}else if(G > previous_of_past_of_G){
								G = ((rand()%previous_of_past_of_G-(previous_of_past_of_G))+( one_past_of_G +1));
							}
						}
					}
					cout << "(temper == 1){\n" <<
									"\tprevious_of_past_of_G < one_past_of_G && one_past_of_G > G \n" << G;
				}else if(previous_of_past_of_G > one_past_of_G && one_past_of_G > G){
					if(temper_of_previous == 1){
						if(temper_of_pastpast == 1){
							G = ((rand()%G-(G))+(one_past_of_G+1));
						}else{
							G = ((rand()%G-(G))+(previous_of_past_of_G+1));
						}
					}
					cout << "(temper == 1){\n" <<"\tprevious_of_past_of_G > one_past_of_G && one_past_of_G > G \n" << G;						
				}
			}
		}else if(n < 2){
				if(temper == 1){
					if(one_past_of_G <= G){
						G = ((rand()%one_past_of_G-(one_past_of_G))+(G+1));
					}
					else if(one_past_of_G > G ){
						G = ((rand()%G )+1);
					}
						cout << "****ing 2 \n";
				}else if(temper == -1){
					if(one_past_of_G <= G){
						G = ((rand()%G )+1);
					}else if(one_past_of_G > G ){
						G = ((rand()%one_past_of_G-(one_past_of_G))+(G+1));
					}
				}
				//cout << "fuching 2 \n";
			}
			
			if((n > 2) && temper == -1){
				if(previous_of_past_of_G < one_past_of_G && one_past_of_G < G){
					if(temper_of_previous == 1){
						for(i = 0; test < G && test > previous_of_past_of_G || i < 6; i++){
								test = ((rand()%previous_of_past_of_G-(previous_of_past_of_G))+(G+1));
								if(test < G && test > previous_of_past_of_G)
								{
									G = test;
								}
							}
						}else if(temper_of_previous == -1){
							
							G = ((rand()%one_past_of_G+1));
						}
							cout << "fuching cold!! 1<2<3\n";
					}else if(previous_of_past_of_G > one_past_of_G && one_past_of_G < G){
						if(temper_of_previous == 1){
								G = ((rand()%one_past_of_G+1));
						}else if(temper_of_previous == -1){
							for(i = 0; test < previous_of_past_of_G && test > one_past_of_G || i < 6; i++){
								test = ((rand()%one_past_of_G-(one_past_of_G))+(previous_of_past_of_G+1));
								if(test < G && test > previous_of_past_of_G)
								{
									G = test;
								}
							}
						}
						cout << "fuching cold!! 1>2<3\n";
					}else if(previous_of_past_of_G < one_past_of_G && one_past_of_G > G){
					if(temper_of_previous == 1){
							if(G <= previous_of_past_of_G){
								for(i = 0; test < one_past_of_G && test > G || i < 6; i++){
									test = ((rand()%G-(G))+(one_past_of_G+1));
									if(test < one_past_of_G && test > G){
										G = test;
									}
								}
							}else if(G > previous_of_past_of_G){
								for(i = 0; test < one_past_of_G && test > previous_of_past_of_G || i < 6; i++){
									test = ((rand()%previous_of_past_of_G-(previous_of_past_of_G))+(one_past_of_G+1));
									if(test < one_past_of_G && test > G){
										G = test;
									}
								}
							}
						}
						cout << "fuching cold!! 1<2>3\n";
					}else if(previous_of_past_of_G > one_past_of_G && one_past_of_G > G){
					if(temper_of_previous == -1){
							G = (rand() % G) + G;
						}
					if(temper_of_previous == 1)
					for(i = 0; test < one_past_of_G && test > G || i < 6; i++){
									test = ((rand()%G-(G))+(one_past_of_G+1));
									if(test < one_past_of_G && test > G){
										G = test;
									}
								}
								cout << "fuching cold!! 1>2>3\n";
					}						
				}
		if(n > 1){
			previous_of_past_of_G = one_past_of_G;
			temper_of_pastpast = temper_of_previous;
		}
		one_past_of_G = G;
		temper_of_previous = temper;
		//cout << "G = " << G;
		//cout << "one past of G = " << one_past_of_G << '\n';
		//cout << "previous of past of G = " << previous_of_past_of_G << '\n';
	}
	cout << "k = " << k << "\n\n";
	cout << "n = " << n << '\n';
	cout << "real = " << real << "\n\n";
	return G;
}

If your are interested
this is the part of second

#include<cstdlib>
//#include<iostream>
//using namespace std;
unsigned int real=12;
unsigned int one_past;
static unsigned int k = 0;
void inputofreal();

//cout << "the real is " << real << '\n';

short int Guess(unsigned int G){
	k++;
	if(k == 1){
		return 0;
	}
	if(G == real){
		return 0;
	}if(G == (real - 1) || G == (real + 1)){
		return 1;
	}
	if(abs(G - real) < abs(G - one_past)){
		return 1;
	}else if(abs(G - real) > abs(G - one_past)){
		return -1;
	}
	one_past = real;
}

and for the people that more interested in problem to solve ...
here is it
Task: Guess

Jill is thinking of a number between 1 and N, and Jack wants to guess it by asking Jill questions of the form "Is it bigger than K?" for K between 1 and N.
You are to implement a procedure play(N) that implements Jack's role in the game. Your implementation should repeatedly call the procedure bigger(K), which is implemented by the grader. bigger(K) will return 1 if Jill's number is greater than K; otherwise it will return 0. Jill's number should be returned by your implementation as the result of play.

Subtask 1 [50 points]

Assume that N=16. Your implementation must use at most 15 calls to bigger and must return the correct result. The implementation files described below contain a correct implementation of this subtask.
Subtask 2 [50 points]

Assume that N=16. Your implementation must use at most 4 calls to bigger and must return the correct result.
Implementation Details

Use the RunC programming and test environment
Implementation folder: /home/ioi2010-contestant/guess/ (download prototype here)
To be implemented by contestant: player.c or player.cpp or player.pas
Contestant interface: player.h or player.pas
Grader interface: grader.h or graderlib.pas
Sample grader: grader.c or grader.cpp or grader.pas and graderlib.pas
Sample grader input: grader.in.1 grader.in.2
Note: The sample grader reads N and Jill's number from standard input.
Expected output for sample grader input: grader.expect.1 grader.expect.2
Compile and run (command line): runc grader.c or runc grader.cpp or runc grader.pas
Compile and run (gedit plugin): Control-R, while editing any implementation file.
Submit (command line): submit grader.c or submit grader.cpp or submit grader.pas
Submit (gedit plugin): Control-J, while editing any implementation or grader file.
CPU time limit: 10 seconds
Memory limit: 256 MB

I can't tell if this is all you are looking for (as you posted a boatload of other stuff) but check out http://www.daniweb.com/forums/post9554.html#post9554 and scroll about 1/4 page down and that will tell you how to get a number within a range.

To be implemented by contestant: player.c or player.cpp or player.pas

It's reassuring that someone, somewhere is still using Pascal :)

The technique that jonsca describes is widely used, and I'll bet that most of the people who use it fail to realize that it doesn't quite work: The numbers yielded by rand()%n are not uniformly distributed unless n is an exact divisor of RAND_MAX+1.

I believe that if n is not a divisor of RAND_MAX+1, there is no way to solve the problem correctly without using a loop (or an equivalent recursive function call). With that understanding, here is a function (slightly modified from page 135 of Accelerated C++) that takes an argument n and returns a uniformly distributed random number that is >= 0 and < n:

int nrand(int n)
{
    assert (n >= 0 && n <= RAND_MAX);
    const int bucket_size = RAND_MAX / n;
    int r;

    do r = rand() / bucket_size;
    while (r >= n);

    return r;
}

Indeed, Narue's tutorial is detailed and useful.

However, it does not cover the point that I was trying to make in my earlier note, which is that unless n is a perfect divisor of (RAND_MAX+1), I don't know of any way of determining a uniformly distributed random number k such that 0<=k<n with a single call to the rand function. In fact, I'm not sure it is even possible to put an absolute upper bound on the number of calls to rand that will be needed in a particular case.

Of course, if numbers that are almost, but not quite, uniformly distributed are good enough, then it's easy.

Thank you guys, I got it, jonsca gave me a link .. there I saw how to do it, and I made a function, that passes lesser value, and bigger value, and returning rand number
thank you arkoenig, that was a great idea.. (RAND_MAX+1)... but RAND_MAX is a very big number, and computer does dividing in minusing divisor timees a number.. so it it is like this
10/2 = 10 - 2(1) - 2 (2) - 2(3) - 2(4)- 2 (5)
how effective is that for RAND_MAX/2 :)?
I think rand() is not a fast working function(power up with STL), however I used it..
I'm not a pro to implement my own rand() function...

but RAND_MAX is a very big number, and computer does dividing in minusing divisor timees a number.. so it it is like this
10/2 = 10 - 2(1) - 2 (2) - 2(3) - 2(4)- 2 (5)
how effective is that for RAND_MAX/2 :)?

1) RAND_MAX is not always a very big number; I've seen C++ implementations on which it was 32767.

2) I think you are mistaken about how computer arithmetic works.

I can prove you my condition..
Now look friend
any computer knows only 1 and 0
10 = 1010 (in boolean)
2 = 10
so how can you pretend
1010/10
?

I have no idea what you're talking about.

However, if you're trying to convince me that the only only way to divide two integers is by repeated subtraction, then you should learn something about shifting.

Using shifting and subtraction, it is very easy to come up with an algorithm that divides two integers in a reasonable amount of time. In practice, hardware designers are more clever than that.

If you are worried about how long it takes to divide one integer by another, I suggest you write a program to measure how long it actually takes, and then stop worrying.

If, on the other hand, you mean something else entirely, then I'm sorry to say that you're going to have to explain it more clearly if you want me to understand it.

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.