15,963,866 members
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
jeron1 27-Oct-21 12:28pm
I think you're missing a couple of exclamation points. :-|

## Solution 1

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.

## Solution 2

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.

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

Top Experts
Last 24hrsThis month
 OriginalGriff 30 Chris Copeland 5 jackspero18 5 Mehran Hoodeh -6
 OriginalGriff 248 Pete O'Hanlon 150 merano99 80 Andre Oosthuizen 55 den2k88 50

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