HELP PLEASE!!! inputting range of array and getting random numbers help

User has to input [a,b] range and I have to declare array of size 400 and program should output random numbers from this range. I am new at this and can you tell me some tips how can I do it. The code I have so far is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include<cstdlib>
using namespace std;

int main () {

    int a,b;

    cin>>a>>b;

    srand(time (NULL));
    
    int array[400];
    cout<<array[rand()];



}
Last edited on
i have changed it a little bit:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 
#include <iostream>
#include<cstdlib>
using namespace std;
int getRandom(){
    int a,b;
    srand(time(NULL));
    if(rand()>a && rand()<b){
    
        return rand();
    
    }



}
int main () {

    int a,b;

    cin>>a>>b;

    srand(time (NULL));
    
    int array[400];
    for(int i=0;i<b-a;i++){array[400]=getRandom();
    }

    cout<<getRandom();
}

In your main you are creating a second random number, this is going to cause some problems for you later I assume.

also,
in your for loop your logic is a bit wrong. You are basically assigning the 400th index your random number every time your loop runs. You are trying to assign 1 value to EVERY index, not 1 value to 1 index every time. It should look like this::

1
2
3
4
5
6
7
8
	int a = 1, b = 10;
	int array[400];

	for (int i = 0; ((i<a) && (i<b)); i++)
	{
		array[i] = getRandom(a, b);
		cout << array[i];
	}


Notice how I am also sending the 'getRandom' function your variables 'a' and 'b'. You have to do this, otherwise you are just going to get compilation errors or irrelevant data.
Last edited on
I have changed it however it does not compile and I do not understand why?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include<cstdlib>
using namespace std;
int getRandom(int a,int b){
    srand( time(NULL) );
    if(rand()>a && rand()<b){
        
        return rand();
        
    }
    
    
    
}// it says that here is an error
int main () {
    
    int a = 1, b = 10;
	int array[400];
    
	for (int i = 0; ((i<a) && (i<b)); i++)
	{
		array[i] = getRandom (a, b);
		cout << array[i];
	}
}
What error are you getting?
control may reach and of void function
to have an array full of random integers you need to use a for loop and know how the rand function works. when the rand statement creates a random number it goes from 0 and on. when you use % it divides it by the HighNum and returns the remainder, then it adds LowNum.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
	int a, b;

	cin >> a;
	cin >> b;

	srand(time(0));

	int myArray[400];
	for (int i = 0; i <= 400; i++)
	{
		int HighNum = b - a;
		int LowNum = a + 1;
		int randomnumber = rand() % HighNum + LowNum;
		myArray[i] = randomnumber;
		cout << myArray[i] << ",";
	}
	return 0;
}
Last edited on
In your getRandom function, have you considered what might happen if your 'if' statement fails? You have a return only for if this 'if' statement runs.

What do you do when these conditions are false?
look at line 20. you only loop when i is less than 1 and less than 10 so it should only loop once?
Your use of getRandom() shows you don't understand how functions work. The error you are getting is likely something to the tune of "not all control paths return a value".

Consider the following execution path:
You enter getRandom(), with a=100 and b=200.
You seed the RNG with the time.
You call rand() for the first part of the condition; suppose it returns 50.
Well, that's certainly less than or equal to a (100), so we don't enter the if statement.
But then we hit the end of the function, and while it was declared to return an int, we aren't returning anything!
See the issue?

Even if that wasn't the problem, each of your calls to rand() is separate and independent so if you are (as it seems) using this to generate a random number between a and b, it will fail because each of the calls to rand() will likely return a completely different number.
I only learned functions today by myself and is not there any way to solve this problem without using a function??
Yes there is, but you need to think about it. Consider what I've told you and you should be able to come up with a solution.
I only learned functions today by myself


if you are still learning about functions you should not start on random number generators
Oohh I understand what you have done in the code upwards and I think that is exactly what I needed without and functions and complexity.
Thank you very much for help I see now my mistakes and it is better not to use functions when I do not know them well. Thankyou very much you have been very helpful
Topic archived. No new replies allowed.