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

Started by
102 comments, last by laserbeak43 16 years, 6 months ago
hi, i want to load random integers into an array. i was thinking of using a for loop to increment through the array and generate a random number for each element. but, i don't know how i'd go about doing that thought....
__________________________________________________________Maybe one day i can not be affraid of venturing out of the beginners section.
Advertisement
You start by mentioning which language you're using...
Quote:Original post by Antheus
You start by mentioning which language you're using...


lol, sorry, i was just about to edit the topic to C++ :P
__________________________________________________________Maybe one day i can not be affraid of venturing out of the beginners section.
Seems like a good place to use the std::generate() function.
Quote:Original post by laserbeak43
hi,
i want to load random integers into an array. i was thinking of using a for loop to increment through the array and generate a random number for each element.
but, i don't know how i'd go about doing that thought....


What are you currently able to do?

Can use make an array? Can you use a for loop to visit each element? Can you set each element to a constant? Can you create a single random number?

Have you any code written yet?
so far, i have this

[source = 'c++']#include <iostream>#include <stdlib.h>using namespace std;int main(){    int xInc;               //gonna try to load this into x[]    float x[511];           //The signal is held in x[0] to x[511]    string meanStr;         //display the result    string SdStr;           //display the result(standard deviant)    n = 512;                //we'll start with a constant value like the book    //call mythical subroutine here   for(i = 0; i > 12; i++)   {       xInc = rand();       x[xInc++];       cout << x << endl;   }	return 0;}


-edit-
it compiles fine, just nothing is shown in the console window.
__________________________________________________________Maybe one day i can not be affraid of venturing out of the beginners section.
Quote:Original post by rip-off

What are you currently able to do?

Can use make an array?

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

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

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;}

Quote:Can you create a single random number?

not exactly sure if i'm doing that part right...
Quote:Have you any code written yet?

yes.
__________________________________________________________Maybe one day i can not be affraid of venturing out of the beginners section.
As Sicrane said...
std::generate(x, x+length, std::rand);std::copy(x, x+length, std::ostream_iterator<int>(std::cout, " "));

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:
float x[511]; //The signal is held in x[0] to x[511]

I think you want float x[512] here.
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;   }

This topic is closed to new replies.

Advertisement