GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Jan-2004, 20:02
NiXeN NiXeN is offline
New Member
 
Join Date: Jan 2004
Posts: 1
NiXeN is on a distinguished road

Random() : Make each number onlu appear once


Forst of all sorry about the empty post, /n you know

I have a sort of dumb a question. I have tried to solv a problem for a "while" now, without any progress.
Newbeeish as I am, only small meaningless programs are the fruits of my labour (wierd sentence).

For once I am trying to build something bigger, a quiz actually. The thing is; the questions are choosed randomly, but not that "well"... The same numbers are picked once in awhile, resulting in the same questions beeing asked (in the same session).

I have tried avoiding this by adding the allreaddy-picked numbers to an array or a vector, and then making the computer check these "registers". If the number is used => generate a new number.

My lack of syntax- and C++- knowledge are the problem
CPP / C++ / C Code:
for (int x=0;x<SIZE;x++) {

bool comp=false, first=true;
vector<int> tilstand;
int valg=rand()%32;

while ((comp==false)&&(first==false)) {
for (int y=0;y<tilstand.size();y++) {
      if (valg!=tilstand[y]) comp=true;
   }
   if (comp!=true) valg=rand()%32;
}

first=false;
comp=false;
tilstand.push_back(valg);

cout<<valg<<endl;
}
  #2  
Old 08-Jan-2004, 09:49
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
The definition of a 'perfect' random number generator, is one which never repeats a number! But how do introduce a unpredictable element into the definate logic of a computer? Computer's generators are all pseudo, based on very funky math, and a 'seed' - but repetitions do occur.

You need a seed to make it even more random! You seed it using the srand(int) function, and pass it something unpredictable (the time is good) by srand( time(NULL) ).

But your biggest problem is that you've so few options to choose from. Write yourself a quick program printing the randon number mod 32 a few times, and you'll see the same number will come up a few times.

GF
  #3  
Old 08-Jan-2004, 14:48
cs2 cs2 is offline
Member
 
Join Date: May 2003
Location: California
Posts: 107
cs2 will become famous soon enough
Quote:
Originally Posted by Garth Farley
The definition of a 'perfect' random number generator, is one which never repeats a number!
Umm... not to split hairs, but that is not the definition of a perfect random number generator. A Google search for "perfect random number generator" produces the following page as first result:
http://www.cryptography.com/resource...s/IntelRNG.pdf (ref. top of column 2 on page 1)

random != unique
__________________
The Whole Internet, LLC
Visit our Homepage, -or-
use our online CSS Editor
  #4  
Old 13-Jan-2004, 04:47
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
I was thinking about that after I wrote it, and you're correct. A purely random number generator is one by which there is an equal probabillity of each number to appear. Statistically, randomness fits the bell curve, so repetitions are allowed. Apologies

GF
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find the last 6 digits of a 8 digit number? rjd72285 C++ Forum 3 28-Oct-2003 08:21
Need a script for selecting random tabels and from those tabels selecting random ques mlt MySQL / PHP Forum 2 12-Sep-2003 09:01
auto number skyloon MySQL / PHP Forum 1 30-Jun-2003 08:33
Random no longer JUST random... JdS GIDTopsites™ 0 12-Jan-2003 08:57

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:52.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.