Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have declare in main the int variable array abc [5], i wish to fill it with 5 random numbers between 0 - 4. Below code fill the array with the same number, let s say 33333. I want 23442 or 11133 or 34341 and so on

What I have tried:

C++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int  CarteACaso ()
{
 int NumeroCarte;
srand((int)time(0));
NumeroCarte= (rand() % 4) + 1;
return NumeroCarte;
}


int main()
{
int gioco[5];
  
 for(int P=0; P<5; P++)
{ int Prova;
Prova=CarteACaso();
gioco[P]=Prova;
cout<<"gioco  "<<P<<"  "<<gioco[P]<<endl;
}

return 0;
}
Posted
Updated 5-Mar-20 0:08am
v2
Comments
KarstenK 5-Mar-20 6:09am    
tip: use srand() only once. It is short for "start randomize" :-O

You are invoking srand each time you get a random number. This "seeds" the rand function. Although you are using time(0), your code will run so fast that time(0) probably has the same value each time, when cast as an int. This will cause rand to return the same value each time. Move the call to srand to the beginning of main and things will probably work out.
 
Share this answer
 
v2
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int  CarteACaso () {
	int NumeroCarte;
	srand((int)time(0));
	NumeroCarte= (rand() % 4) + 1;
	return NumeroCarte;
}

int main() {
	int gioco[5];
	for(int P=0; P<5; P++) {
		int Prova;
		Prova=CarteACaso();
		gioco[P]=Prova;
		cout<<"gioco  "<<P<<"  "<<gioco[P]<<endl;
	}
	return 0;
}

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
 
Share this answer
 
As mentioned, the srand call needs to moved out of that function and into main so it is called once.

You also have a problem with how you are using the return from rand(). You stated you want random values between 0 and 4. The function returns random values from 0 to RAND_MAX with is typically 32K. You are adding one to the result of the return modulo 4. This means the values will range from 1 to 4. Here is a function that will return values within the range specified.
int GetRandomValue( int minval, int maxval )
{
    int range = maxval - minval + 1;
    int rval = rand() % range;
    return rval + minval;
}
You want numbers that range from 0 to 4 so call this function and pass the values 0 and 4 to it.

Here's code that will test of that function :
C++
void DoRandomValueTest()
{
    std::vector< int > values( 10, 0 );
    const int count = 1000;
    for( int n = 0; n < count; ++n )
    {
        int rv = GetRandomValue( 1, 8 );
        values[ rv ] += 1;
    }

    size_t valueCount = values.size();
    trace( _T( "random value vector has %d items\n" ), (int) valueCount );
    for( size_t i = 0; i < valueCount; ++i )
        trace( _T( "%2d : %4d times\n" ), (int) i, values[ i ] );
}
Here is the output from the test :
21:04:02.279 TestThread_07   random value vector has 10 items 
21:04:02.279 TestThread_07    0 :    0 times 
21:04:02.279 TestThread_07    1 :  129 times 
21:04:02.279 TestThread_07    2 :  143 times 
21:04:02.279 TestThread_07    3 :  116 times 
21:04:02.279 TestThread_07    4 :  113 times 
21:04:02.279 TestThread_07    5 :  119 times 
21:04:02.279 TestThread_07    6 :  135 times 
21:04:02.279 TestThread_07    7 :  129 times 
21:04:02.279 TestThread_07    8 :  116 times 
21:04:02.279 TestThread_07    9 :    0 times
As you can see, all values are within the range of 1 to 8. The distribution is reasonably flat for such a small range.
 
Share this answer
 
v2
hello guys thanks for your valuable tips.
about indentation, i have been reading the links but i got lost, there are lot of styles.
i ll try to use the one you suggested in the post. i am using dev c++.
as you realized, i am a beginner.
i am very happy having known this site. it's vital for my learning. likely i ll submit bunch of questions, this the only way i have to learn. thank you again.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900