473,388 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

random function in loops

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
3 2032
<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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Andreas Lobinger | last post by:
Aloha, i wanted to ask another problem, but as i started to build an example... How to generate (memory and time)-efficient a string containing random characters? I have never worked with...
10
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I...
1
by: steflhermitte | last post by:
Dear cpp-ians, I want to apply a stratified sampling on an image. Following simplified example will explain my problem. The original image I with nrows and ncols is now a vector V of length...
11
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number...
12
by: Joseph Shraibman | last post by:
Is there a way to get random rows besides ORDER BY random()? The problem with ORDER BY random() is that is has to get all the rows from the table before the results are returned. ...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
19
by: Erich Pul | last post by:
hi! i got a structure, which should be filled with random integer values (it is in fact a generator for numbers like in a lotto), but these values must not be recurring, so no double occurrences...
2
by: Brendon Towle | last post by:
I need to simulate scenarios like the following: "You have a deck of 3 orange cards, 5 yellow cards, and 2 blue cards. You draw a card, replace it, and repeat N times." So, I wrote the...
15
by: caca | last post by:
Hello, This is a question for the best method (in terms of performance only) to choose a random element from a list among those that satisfy a certain property. This is the setting: I need to...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.