Re: rand() function and array Programming Software Development by Ancient Dragon rand() returns integers, not doubles or floats, so you might as well declare setNum as an array of integers. And the code you posted does not even reference that array nor does it ever call rand() rand() Help Programming Software Development by God Coder123 If i had an Array as seen below, is it possible to use the rand function to randomly select an element within that particular array; if so how can this be done. The Value of the element will be outputted. Any help much appreciated...:) [CODE]int Array[14]={10,10,10,10,11,6,9,2,3,15,11,15,16,15};[/CODE] Re: rand() Help Programming Software Development by daviddoria Does this help? [code] int RandomInt(const int MAX) { //produce an int from 0 to MAX-1 return rand() % MAX; } [/code] Dave Re: rand() Help Programming Software Development by Aranarth [code]const int arraySize=sizeof(Array)/sizeof(*Array); cout << Array[rand()%arraySize] << endl;[/code] Re: rand() Help Programming Software Development by Sandhya212 … namespace std; void seed(){ srand(time(0)); } double Randunif(){ return rand() / double(RAND_MAX); } double Randunif(double x){ return ((Randunif()*x)+1… Re: rand() Help Programming Software Development by God Coder123 Thanks Guys. This method works great, thanks Aranarth. Here's what i got: [CODE]srand ( time(NULL) ); int Array[]={1,2,6,7,8,89}; const int arraySize=sizeof(Array)/sizeof(*Array); cout << Array[rand()%arraySize] << endl;[/CODE] Although i don't really understand the method, Perhaps you could enlighten me Aranarth. Re: rand() Help Programming Software Development by God Coder123 Sweet, i see what you mean Dave This is what ive got: [CODE]vector<int>array; array.push_back(1); array.push_back(2); array.push_back(6); const int arraySize=array.size(); cout << array[rand()%arraySize] << endl;[/CODE] BTW what does the .size(); function actually do here. :) rand() Programming Software Development by nitin1 can you please tell me how rand () is implemented ? i know it is library implementaion dependent. but … Re: rand() Programming Software Development by Moschops … been done, in glibc Version 2.15; the call to rand ends up at the function __random_r in this implementation http… Re: rand() Programming Software Development by deceptikon > but still can you tell me the way in which it is mostly implemented ? rand() is usually implemented with some form of [linear congruential generator](http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_rand.aspx). rand() command showing error when written inside struct Programming Software Development by coder_naive …, the computer is showing syntax error at line int P = rand(); ... while the same line, if I write in the main…> #include <math.h> struct poclick { int P = rand(); }; int main () { int num = 4; // number of cases int nj… Re: rand() command showing error when written inside struct Programming Software Development by sfuo … { int P; }poclick; void poclickInit(poclick *in) { in->P = rand(); } int main() { poclick A; A.P = 0; //directly modifying the… Re: rand command Programming Software Development by deceptikon … i = 0; i < 1000; i++) { double r = lo + uniform_deviate(rand()) * (hi - lo); int block = i + 1; printf("%f "…'t lose precision on the floating point value due to `rand` being strictly integer based, and I'd like to think… rand command Programming Software Development by alon4963 How to set rand command, an integer from 3 to 50 (inclusive), but save it as a decimal number Re: rand command Programming Software Development by DavidB … of my head, I'd probably use two variables. Call rand() and assign its output to an integer variable, say, *temp_int… Rand Max Problem Programming Software Development by punter …; countoccurence(1); } ] int generatehighvalue (int valuetrials) [ int num, compare; num = rand(); compare = 0; if (trials > 1) compare = generatehighvalue(trials - 1… 3260 8 3295 9 3330 missing 0 of 10 Testing rand()%n for n = 16 0 2073 1 1983 2 2030… Rand in a 2D array, duplicating locations! Programming Software Development by Leppie …; p<100 ; p++ ) { randx =rand()% 10; randy =rand()% 10; array_check[randx][randy]=1; if (p ==…p << endl; //do { // randx =rand()% 10; // randy =rand()% 10; // if (array_check[randx][randy]!=2) // …=0; n<=98; n++) // { // randx =rand()% 10; // randy =rand()% 10; // array_check[randx][randy]=1; // } //for … Re: Rand in a 2D array, duplicating locations! Programming Software Development by NathanOliver …zeroY; int map[10][10]; srand((unsigned)time(0)); zeroX = rand() % 10; // this gets the spot to put the zero …zeroY = rand() % 10; // place zero map[zeroX][zeroY] = 0; while (counter &…lt; 100) { x = rand() % 10; y = rand() % 10; if (x == zeroX && y == zeroY) //… Re: Rand in a 2D array, duplicating locations! Programming Software Development by Leppie …but it didn't solve the problem as rand still overwrites itself in your code as well.…each element with a 0. I then use rand to randomly select an element and change that…are never 10 lots of 1. (Duplicity of rand overwrites already placed 1's, leaving me less… therefore need to check the value of where rand will place the number 1 before its actual … Re: Rand in a 2D array, duplicating locations! Programming Software Development by VernonDozier …; 10) // check whether we have ten ones yet { int index = rand () % 20; // pick a random index if (array[index] != 1) // check… Rand function not working very well. Programming Software Development by rkulp …; srand(unsigned(time(NULL))); Temp1 = Dice[(rand()%16)]; Temp2 = Dice[(rand()%16)]; while (Temp1 == Temp2) { Temp2 = Dice[(rand()%16)]; } Dice[Temp1] = Temp2; Dice… Re: Rand function not working very well. Programming Software Development by justice igwe …; srand(unsigned(time(NULL))); Temp1 = Dice[(rand()%16)]; Temp2 = Dice[(rand()%16)]; while (Temp1 == Temp2) { Temp2 = Dice[(rand()%16)]; } Dice[Temp1] = Temp2; Dice…[Temp2] = Temp1; Side[Temp1] = (rand()%6); Side[Temp2] = (rand()%6); }[/CODE][/QUOTE] Note check the srand(unsigned(time… Rand Number isn't Random! Programming Software Development by Jerail …;10;i++) // loop thru 10 times... { numbers[i] = (rand()%100); // get a rand num cout << numbers[i] << endl… to screen (+ linebreak) sum = sum + numbers[i]; // add rand num to total sum if(numbers[i]>=0 &&… Re: Rand function not working very well. Programming Software Development by dusktreader You have some value index confusion happening here I think Try [code=c++] int temp; int idx0 = rand() % 16; int idx1 = idx0; while( idx1 == idx0 ) idx1 = rand() %16; temp = Dice[idx0]; Dice[idx0] = Dice[idx1]; Dice[idx1] = temp; [/code] Re: Rand Array Help please, my code isn't working Programming Software Development by Labdabeta … algorithm has a fatal flaw. You should realize that rand() is random (or at least relatively close to random… would happen to your code if, for example, rand() always returned 4 (http://xkcd.com/221/) of course…<array_size; ++i) { //swap(array[rand()%i],array[i]); int rnd=rand()%i; int tmp=array[rnd]; array[rnd]=array… Re: Rand function not working very well. Programming Software Development by Salem > (I loop it 250 times) ... > srand(unsigned(time(NULL))); Unless your code also takes 250 seconds to run, then srand() is always going to be seeded with the SAME value, and rand() is always going to return the SAME value(s) as a result. srand() needs to be called ONCE only, at the start of main. Re: Rand function not working very well. Programming Software Development by mrnutty … always going to be seeded with the SAME value, and rand() is always going to return the SAME value(s) as… Re: Rand function not working very well. Programming Software Development by hag++ … always going to be seeded with the SAME value, and rand() is always going to return the SAME value(s) as… rand() function without < stdlib.h> header Programming Software Development by ashish2expert … have not defined <stdlib.h> header and included rand() function and it works without this header. How? #include <…("Your hand: "); while(num_cards > 0) { suit = rand() % NUM_SUITS; rank = rand() % NUM_RANKS; if(!in_hand[suit][rank]) { in_hand[suit][rank] = true… Re: Rand Array Help please, my code isn't working Programming Software Development by vijayan121 … ) ; std::srand( std::time(nullptr) ) ; std::seed_seq seed_seq { std::rand(), std::rand(), std::rand(), std::rand() } ; std::shuffle( begin, end, std::mt19937(seed_seq) ) ; for…