random function in loops

  • Thread starter syntheticdesigner
  • Start date
S

syntheticdesigner

I hope someone could correct this simple excerpt
since I fail to see how I can make rand() function works
in a loop manner

/////////////////////////////////
srand(time(NULL));
int r1=rand()%10+1;
for(int o=0;o<r1;++o){
int r2=rand()%10+1;
for(int p=0;p<r2;++p)
{
put_to_array[o][p]=rand()%10;
}
}
////////////////////////////////

when I call the function having this code excerpt,
a row of the 2d matrix put_to_array[][]
always has the same elements as others

i.e

1 2 4 5
1 2 4 5
1 2 4 5

Thanks
 
J

Jim Langston

I hope someone could correct this simple excerpt
since I fail to see how I can make rand() function works
in a loop manner

/////////////////////////////////
srand(time(NULL));
int r1=rand()%10+1;
for(int o=0;o<r1;++o){
int r2=rand()%10+1;
for(int p=0;p<r2;++p)
{
put_to_array[o][p]=rand()%10;
}
}
////////////////////////////////

when I call the function having this code excerpt,
a row of the 2d matrix put_to_array[][]
always has the same elements as others

i.e

1 2 4 5
1 2 4 5
1 2 4 5

You must be outputting it wrong. The output of one run of the following
program for me is:

9 9 2 6 0 8 0 6 8 0 0 0
9 1 4 7 0 1 0 0 0 0 0 0
6 5 5 0 0 0 0 0 0 0 0 0
5 9 6 0 3 9 8 0 3 1 0 0
7 0 8 0 0 0 0 0 0 0 0 0
8 7 4 4 2 1 0 0 0 0 0 0
8 6 7 4 9 8 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0

#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
int put_to_array[12][12] = {0};

std::srand(std::time(NULL));
int r1 = std::rand()%10+1;
for(int o=0;o<r1;++o){
int r2 = std::rand()%10+1;
for(int p=0;p<r2;++p)
{
put_to_array[o][p] = std::rand()%10;
}
}

for ( int i = 0; i < 12; ++i )
{
for ( int j = 0; j < 12; ++j )
std::cout << put_to_array[j] << " ";
std::cout << "\n";
}
std::cout << "\n";


return 0;
}
 
D

Default User

I hope someone could correct this simple excerpt
since I fail to see how I can make rand() function works
in a loop manner

Please post a complete, minimal program that demonstrates the problem.

This has been a recording.





Brian
 
O

Old Wolf

srand(time(NULL));
int r1=rand()%10+1;
for(int o=0;o<r1;++o){
int r2=rand()%10+1;
for(int p=0;p<r2;++p)
{
put_to_array[o][p]=rand()%10;
}}

when I call the function having this code excerpt,
a row of the 2d matrix put_to_array[][]
always has the same elements as others

Perhaps the function is running so fast that
time(NULL) is the same each time, meaning you
get the same random numbers.

You are only supposed to call srand(time(NULL))
once per program run.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top