Menu
Menu
DaniWeb
Log In
Sign Up
Read
Contribute
Meet
Search
Search
About 1,000 results for
rand
- Page 1
Re: rand() function and array
Programming
Software Development
14 Years Ago
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
14 Years Ago
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
14 Years Ago
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
14 Years Ago
by Aranarth
[code]const int arraySize=sizeof(Array)/sizeof(*Array); cout << Array[
rand
()%arraySize] << endl;[/code]
Re: rand() Help
Programming
Software Development
14 Years Ago
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
14 Years Ago
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
14 Years Ago
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
11 Years Ago
by nitin1
can you please tell me how
rand
() is implemented ? i know it is library implementaion dependent. but …
Re: rand()
Programming
Software Development
11 Years Ago
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
11 Years Ago
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 Max Problem
Programming
Software Development
18 Years Ago
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
14 Years Ago
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
14 Years Ago
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
14 Years Ago
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
14 Years Ago
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
15 Years Ago
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
15 Years Ago
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
15 Years Ago
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
15 Years Ago
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
11 Years Ago
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
15 Years Ago
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
15 Years Ago
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
15 Years Ago
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
12 Years Ago
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
11 Years Ago
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…
rand and srand
Programming
Software Development
10 Years Ago
by lewashby
In the program below what is the difference between
rand
and srand? My book says that
rand
returns an int between 0 and RAND_MAX, which is never smaller than 32,767. Yet this code returns a number from 1-6, how is that? I see the line `(
rand
() % 6 ) + 1` but I don't see how that would give a number from 1-6.
Re: rand and srand
Programming
Software Development
10 Years Ago
by deceptikon
> In the program below what is the difference between
rand
and srand? `
rand
` generates a pseudorandom number. `srand` seeds the generator that… `
rand
` uses. The seed essentially determines what the first random number …
Re: rand() range help
Programming
Software Development
17 Years Ago
by joshua.tilson
> `int high=10;` > `int low=6;` > `
rand
() % (high - low + 1) + low;` Hello, I was hoping some one could shed some light on the reason this works. I have used
rand
() in the past to simulate rolling dice (1-100 etc.). I have never used the minimum number like this. why is it that `
rand
() % 11` = a range of 6 - 10? Thank you!
Re: Rand Number isn't Random!
Programming
Software Development
15 Years Ago
by jonsca
[icode]#include <ctime>[/icode] and call [icode] srand((unsigned)time(0)); [/icode] (once) before you call
rand
().
Re: rand() function and array
Programming
Software Development
14 Years Ago
by Ancient Dragon
… loop? The while loop should do nothing more than call
rand
() for each element of the array. There is no point… to change all the elements to the value returned by
rand
(). >>line 27 I belive I called…
1
2
3
17
Next
Last
Search
Search
Forums
Forum Index
Hardware/Software
Recommended Topics
Programming
Recommended Topics
Digital Media
Recommended Topics
Community Center
Recommended Topics
Latest Content
Newest Topics
Latest Topics
Latest Posts
Latest Comments
Top Tags
Topics Feed
Social
Top Members
Meet People
Community Functions
DaniWeb Premium
Newsletter Archive
Markdown Syntax
Community Rules
Developer APIs
Connect API
Forum API Docs
Tools
SEO Backlink Checker
Legal
Terms of Service
Privacy Policy
FAQ
About Us
Advertise
Contact Us
© 2025 DaniWeb® LLC