Question:
Help with rand() in c programming visual studios?
2012-08-30 21:14:49 UTC
Okay well I'm trying to use the rand() function in visual studio 2010 and I want to make sure that it "throws" or picks 10,000 random numbers inbetween 1-12 and then gives an output in 1 go, then I want to do it 11 more times, but I can't seem to get it to work.. I'm quite new to programming too... (This is c, not c++)
Five answers:
Jared
2012-08-30 21:28:52 UTC
what is not working?



Edit:



Others have given some code, so I'll just add this. I wasn't sure what the problem was, but one thing you should be careful about with C/C++ is that if you don't randomly seed the random number generator (i.e. with the system clock) then you will NOT get "random" results. I use quotes because it will give the same "random" results every time you run the program. So if you want the program to give random results every time you run the program (as opposed to the same results every time) you have to seed the random number generator--which is usually done with a call to get the system time (since that should be random each time you run the program).



One more thing. If this is for an assignment they might not want you to do the above because it's easier to grade if they know what random results you will get. So if it's for an assignment, check with your teacher to see whether or not they want you to randomly seed the random number generator.
Anas Imtiaz
2012-08-31 12:39:45 UTC
Complete working code in C. Checked it myself in Visual Studio 2010. Proper comments to explain the code.



#include

#include



int main()

{

srand(time(NULL)); //initializing the rand() function with current system time

int random_numbers[10000] = {0}; //integer array to hold 10000 random numbers at once

int i = 0, j; //for indexing through the loops

char continue_key; //for continuing the loop

while(i < 11) //repeating the procedure 11 times

{

//generating random numbers

for(j = 0; j < 10000; j++)

random_numbers[j] = rand() % 12 + 1;

//printing 10000 random numbers in 1 go

for(j = 0; j < 10000; j++)

printf("%d ", random_numbers[j]);

//asking user to press key to continue.

//we add this so that the program pauses for a while so that user can see the numbers generated each time the loop is iterated.

//otherwise, the program will keep on running until it has produced 110000 random numbers and displayed them

printf("\nEnter any key to continue:\t");

scanf("%c", &continue_key);

i++;

}

return 0;

}



For queries, email or IM me.
arregui
2016-07-26 23:51:51 UTC
Transfer: sum = a + compNum; var = sum % 2; to a position AFTER the participant has made his first opt for, since while you do it the place your whole variables are declared, sure, a has no longer been initialised to a value and so this line fails. That is C++ so there is no have got to declare the entire variables at the identical location. That seems to have careworn you. Additionally in the case of bizarre i do not see you do a cin to get the participant's price, like you do for Even or default.
Tuan N
2012-08-30 21:33:12 UTC
Well the code in c++ is: (I'm using c++ not c)



for(int i=0; i<10000; i++)

{

int randomNumber = rand() % 12 + 1;

cout<<"["<
}



This random code should generate 10,000 random numbers between 1-12.

You also should know that the code is in c++. All you have to do is change the cout<< operator to

printf() and it should be C code.



-T
2012-08-30 21:38:20 UTC
So basically you want your program to emit 10,011 'random' integers within the inclusive range of 1 to 12.



#include

#include



/* Generates a number from [0, n - 1] inclusive */

#define randn(n) (rand() / (RAND_MAX / (n)))



/* Generates a number from [min, max] inclusive */

#define random(min, max) (randn((max) - (min) + 1) + (min))



int main(void)

{

        int i;



        for (i = 0; i < 10011; i++)

                printf("%d\n", random(1, 12));

        return 0;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...