Click here to Skip to main content
15,741,688 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I want to create a 2D array data with 100 row and 100 column in which every cell would be a float number between 0 and 10 randomly generated, store row by row. After that implement selection and range queries on it.


What I have tried:

I have done nothing much. just got confused about it!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Posted
Updated 27-Oct-21 11:42am
Comments
jeron1 27-Oct-21 12:28pm    
I think you're missing a couple of exclamation points. :-|

I use this function to generate random values :
C++
// obtain a random value scaled to be within limits

template< typename T >
inline T GetRandomValue( T minval, T maxval )
{
	return ( rand() * ( maxval - minval ) / (T) RAND_MAX ) + minval;
}
Converting that to a non-template function, it can look like this :
C++
// obtain a random value scaled to be within limits

inline float GetRandomValue( float minval, float maxval )
{
	return ( rand() * ( maxval - minval ) / (float) RAND_MAX ) + minval;
}
then you can call it like this :
C++
float value = GetRandomValue( 0.0f, 10.0f );

Remember to seed the PRNG with srand. Here is one way :
C++
short seed = short( time(NULL) % RAND_MAX );
srand( seed );
for a unique sequence almost every time. Seed it with the same value for a repeatable sequence, preferably a prime number like 43.
 
Share this answer
 
You can declare the array thus:
C++
float floatArray[100][100];

The array is indexed by expressions of the form floatArray[row][col], where row and col are integer offsets between 0 and 99. You can use srand/rand to generate random numbers.
 
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