C++: load random numbers into an array with a for loop?

Started by
102 comments, last by laserbeak43 16 years, 7 months ago
Quote:Original post by Washu
As Sicrane said...

std::generate(x, x+length, std::rand);


cool, was googling for info on that, but couldnt find anything that made sense to me. thanks.

also should it be, std::generate(x, x+length, std::rand);
or std::generate(x, x+length, std::rand);?
i'm sorry, anyone know of a doc with this in it?
__________________________________________________________Maybe one day i can not be affraid of venturing out of the beginners section.
Advertisement
Quote:Original post by SiCrane
Quote:
float x[511]; //The signal is held in x[0] to x[511]

I think you want float x[512] here.


you're right thanks
__________________________________________________________Maybe one day i can not be affraid of venturing out of the beginners section.
Quote:Original post by Zenix
You're never assigning values to x, and your for loop condition was wrong.

   for(i = 0; i < 12; i++)   {       x = rand();       cout << x << endl;   }

heh, i must be tired. i didn't notice that, thanks

__________________________________________________________Maybe one day i can not be affraid of venturing out of the beginners section.
Quote:Original post by laserbeak43
float x[511]; //The signal is held in x[0] to x[511]

Don't count on it, more like x[0] to x[510].

Quote:
Quote:Can you use a for loop to visit each element?

like this?
int x[5];for(i = 0; i>5; i++){    cout << x;}

No, watch your operators, this is why you're not getting any output from your program:

int x[5];//....for (int i = 0; i < 5; i++) {    std::cout << x;}


Quote:
Quote:Can you set each element to a constant?

would that be:
int x[5];for(i = 0; i>5; i++){    const myNum;    x = myNum;    cout << x;}

Again, watch your operators, and you're not creating a constant correctly at all:
int x[5];const int myNum = 99;for (int i = 0; i < 5; i++) {    x = myNum;}


Quote:
Quote:Can you create a single random number?

not exactly sure if i'm doing that part right...

Use std::srand(unsigned int seed) to seed the pseudo-random number generator and then use std::rand() to get random values. looky here.
Quote:Original post by laserbeak43
Quote:Original post by Washu
As Sicrane said...

std::generate(x, x+length, std::rand);


cool, was googling for info on that, but couldnt find anything that made sense to me. thanks.

also should it be, std::generate(x, x+length, std::rand);
or std::generate(x, x+length, std::rand);?
i'm sorry, anyone know of a doc with this in it?


It should be as I wrote it. Your version will not compile. You could pass &x[0], and &x[0] + length if you really wanted though.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Quote:Original post by laserbeak43
so far, i have this

*** Source Snippet Removed ***

-edit-
it compiles fine, just nothing is shown in the console window.


There's a few things wrong here. First, your for loop statement by itself:

for(int i = 0; i > 12; i++)

i is less than 12, yet your condition is when i is greater than 12. Switch the '>' for '<' and then your loop will run. However, it won't run correctly because:

xInc = rand();
x[xInc++];

rand() returns an integer between 0 and RAND_MAX. x is an array if 511 elements (you put 511, but x[511] means 0 to 510, inclusive. If you want 0 to 511 you need x[512]). So you have a situation where you will often get a result greater than 512 and then you try accessing the array x outside of it's bounds. You'll want to restrict to range of rand(), possibly like this:

xInc = rand() % 512;

The '%' (modulo) operator returns the remainder of integer division and so the remainder will never be 512 or greater.

Actually, that probably won't cause any problems in your program either because... You never assign anything to any array elements. To assign to your array you probably meant to do this:
xInc = rand() % 512;
x = xInc;

or better yet, just cut out all that junk and do this:

x = rand() % 512;

If you loop from 0 to less than 512 and perform that operation in the loop then you get an array filled with random numbers.

Anyway, I assume that this is homework so I won't give you that solution. Instead I'll give a much more advanced solution that, if you submitted, should raise the grader's 'cheating' flag and that, if it isn't homework, will at least teach you a different way to solve the problem.

#include <iostream>#include <vector>#include <algorithm>#include <cstdlib>#include <ctime>using namespace std;int rand512(){  return rand() % 512;}int main(){  srand(time(0));  std::vector<int> x(512);  std::generate(x.begin(), x.end(), rand512);  for(std::vector::iterator i = x.begin(); i != x.end(); ++i)  {    std::cout << *i << "\n";  }}


Hope this helps.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Quote:Original post by dmatter
Quote:Original post by laserbeak43
float x[511]; //The signal is held in x[0] to x[511]

Don't count on it, more like x[0] to x[510].

Quote:
Quote:Can you use a for loop to visit each element?

like this?
int x[5];for(i = 0; i>5; i++){    cout << x;}

No, watch your operators, this is why you're not getting any output from your program:

int x[5];//....for (int i = 0; i < 5; i++) {    std::cout << x;}


Quote:
Quote:Can you set each element to a constant?

would that be:
int x[5];for(i = 0; i>5; i++){    const myNum;    x = myNum;    cout << x;}

Again, watch your operators, and you're not creating a constant correctly at all:
int x[5];const int myNum = 99;for (int i = 0; i < 5; i++) {    x = myNum;}


Quote:
Quote:Can you create a single random number?

not exactly sure if i'm doing that part right...

Use std::srand(unsigned int seed) to seed the pseudo-random number generator and then use std::rand() to get random values. looky here.



nice thanks! gonan have a look now.
__________________________________________________________Maybe one day i can not be affraid of venturing out of the beginners section.
Quote:Original post by laserbeak43
Quote:Original post by Washu
As Sicrane said...

std::generate(x, x+length, std::rand);


cool, was googling for info on that, but couldnt find anything that made sense to me. thanks.

also should it be, std::generate(x, x+length, std::rand);
or std::generate(x, x+length, std::rand);?
i'm sorry, anyone know of a doc with this in it?


It should be the former not the latter.

You can find documentation here and it even comes complete with an example of what you're tring to do.
It just occurred to me after typing all that you never indicated there was a range limit on your random numbers. In which case just look at every other example that was given.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Quote:Original post by nobodynews


Anyway, I assume that this is homework so I won't give you that solution. Instead I'll give a much more advanced solution that, if you submitted, should raise the grader's 'cheating' flag and that, if it isn't homework, will at least teach you a different way to solve the problem.

*** Source Snippet Removed ***

Hope this helps.


LOLOL, thanks for the tips, and no it's not homework, i'm reading http://www.dspguide.com/ch2/2.htm and trying to translate it to c++.
It's not game related, but you guys are great, so i always come here.
__________________________________________________________Maybe one day i can not be affraid of venturing out of the beginners section.

This topic is closed to new replies.

Advertisement