By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

random function in loops

P: n/a
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

Jun 9 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
<sy***************@yahoo.comwrote in message
news:11**********************@x35g2000prf.googlegr oups.com...
>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[i][j] << " ";
std::cout << "\n";
}
std::cout << "\n";
return 0;
}
Jun 9 '07 #2

P: n/a
sy***************@yahoo.com wrote:
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
Jun 9 '07 #3

P: n/a
On Jun 9, 6:27 pm, syntheticdesig...@yahoo.com wrote:
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.

Jun 10 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.