also @ TechSpot: AMD Radeon HD 7770 & Radeon HD 7750 Review
Welcome to the TechSpot OpenBoards. Please read the FAQ if you have any questions. Sign up or Login to participate.

Go Back   TechSpot OpenBoards > Software > Software Apps

Begin your free trial now Pay-as-you-go options starting at $10/user/month

Programming help - random numbers

Thread Tools Search this Thread
  #1  
Old 10-22-2005
tomyas's Avatar
Newcomer, in training
 
Member since: May 2005, 6 posts
Programming help - random numbers

hi,
i have made a program that will generate a random number until the number i type is generated, but to make it faster i thought it would be helpful to stop the program going over numbers it has already generated, the problem is, i have no idea how to do this.

please help,
Thanks!

lammie12345@yahoo.co.uk
  #2  
Old 10-23-2005
zephead's Avatar
TechSpot Paladin
 
Location: Illinois, USA
Member since: Dec 2004, 2,483 posts
System specs
what language?

PS > do yourself a favor and remove your email address from above post, this is a public webpage.
  #3  
Old 10-23-2005
TechSpot Evangelist
 
Location: has left the building
Member since: Aug 2003, 8,165 posts
Quote:
to stop the program going over numbers it has already generated
That's not really random, now is it?

Quote:
generate a random number until the number I type is generated
It should hardly match, if ever. Again, that's not really random, now is it?

Maybe get some inspiration here:
http://msdn.microsoft.com/library/de...mrandomize.asp
  #4  
Old 10-23-2005
Vigilante's Avatar
TechSpot Paladin
 
Location: Arizona, USA
Member since: Dec 2004, 2,120 posts
Well RBS, good point, unless of course his random number range is like 1 to 20.

I don't think you will "save time" by avoiding the numbers though. Think about it, MORE programming steps have to take place, as well as memory space.

1) You will have to store all the numbers previously chosen, meaning a database of sorts, and thus memory usage.
2) You will have to query this database to see if the current number matches a stored number. Thus processing power.
3) And lastly, the random number has ALREADY been selected, so why waste processing time to see if it's been chosen before? Just check the number against yours and move on.

It's probably fastest the way you're doing it now. UNLESS, there is some long calculation performed on each number selected. In this case, checking the number, and thus avoiding a much more costly and redundant code, a check could be useful. So you have to weigh the 3 items above in terms of memory and processing, against the processing you do on each random number. And come to a conclusion of whether you really need to do this. So please answer the questions above, and enumerated here:

What language? What is the range of random numbers? What kind of processing do you do on each number?
  #5  
Old 10-23-2005
Nodsu's Avatar
TechSpot Evangelist
 
Location: Estonia
Member since: Feb 2002, 9,431 posts
System specs
Unless you are working with a computer from 1970s it should have enough horsepower to generate reasonably random numbers in a blink and store at least thousands of them in a bit field (and reference that in a matter of milliseconds).

What language and what algorithms are you using? What exactly do you want to do? How about providing the source code here?
  #6  
Old 10-23-2005
jobeard's Avatar
TechSpot Ambassador
 
Location: Southern Calif.
Member since: Apr 2005, 10,832 posts
if the goal is a single digit random number, then the value of PI (circumference / diameter) to the maximum precision possible, is a great seed value.

however, that number (3.1416.....) clearly has repeating digits.

Your therom may need to be reevaluated, depending upon your expected usage.
  #7  
Old 10-23-2005
zephead's Avatar
TechSpot Paladin
 
Location: Illinois, USA
Member since: Dec 2004, 2,483 posts
System specs
i think he wants to do integers here...
  #8  
Old 10-23-2005
TechSpot Evangelist
 
Location: Bridgend
Member since: Nov 2003, 2,371 posts
what's the definition of a random number in programming anyway? I mean, if using algorythms, the number is newer really random - I know that's picking holes, but at the same time, it also means that theere must be a programmers definition of 'random'?
  #9  
Old 10-24-2005
Vigilante's Avatar
TechSpot Paladin
 
Location: Arizona, USA
Member since: Dec 2004, 2,120 posts
Actually Spike, you're right, for most purposes, the built-in random generators in most basic languages are not very random. I read a big long article on the subject one time, heck if I remember now though.

I found this snippet while searching around:

Quote:
Most computers (perhaps all computers) don't generate truly random values. To be truly random, the value must not be predictable. However, computers use complex mathematical formulas to generate values which look random, but really aren't. Even though the formulas are complex, they're still predictable.

Conceptually, random numbers generated by a computer can be thought of as a very, very long list of values. The computer always returns the values in the same order, but it doesn't always start at the same place in the list. If the list contained a million values, sometimes the computer would return values starting at the first value, but sometimes it might start at the 105,768th value. Because the starting point is unknown and the list is so long, a repeatable pattern is not discernable.
I know this to be true, I have that effect in my car CD player, it's really weird. When I put in an MP3 CD with, say, about 300 MP3s on it, and tell the CD player to shuffle (i.e. random), it still plays the songs in the same order every time! Sometimes starts on a different song, but the songs play in the same "random" order each time I use the disk. It bugs me actually, as playing random is a specific order, same as just playing start to finish. Each CD plays random, but each CD has it's own random pattern that is always the same. It's odd for sure.
  #10  
Old 10-24-2005
Nodsu's Avatar
TechSpot Evangelist
 
Location: Estonia
Member since: Feb 2002, 9,431 posts
System specs
Modern operating systems usually provide number generation that is truely random for any practical purposes. The "randomness" is collected from peripheral devices like the network card, keyboard (user input is a very random thing - noone can produce the same keystrokes at millisecond accuracy), hard drives etc.
  #11  
Old 10-31-2005
Newcomer, in training
 
Location: South Africa
Member since: Oct 2005, 12 posts
Random numbers

Hi tomyas.

If I understand correctly...
You want to create a program that generated a random number.
For an example (between 0 and 1000).
If you want to avoid generating the same number you should try the following...

This is if I understand correctly...

1) create an array
example: dim myarray(0 to 1000) as integer
2) generate a random number
3) Check the value on the array
if myarray(random_number)=0 then
'this means the number was not previously generated
'now flag it... myarray(random_number)=1
else
'this means it was previously generated
'generate again
end if

Happy Programming...
  #12  
Old 10-31-2005
Newcomer, in training
 
Member since: Oct 2005, 22 posts
programming languages usually have a built in radom number generator, ive used the one in c++ before, it gets the same random numbers every time, to get around this you can "seed" it with something to give it a different starting place and therefore different radom numbers. the easiest thing to do is use the system clock to seed the generator, which unless you got two computer in sync with eachother and got them to generate at the exact same second, you wouldnt get the same numbers....however you can always use something else to seed the generator. If you know how the generator works and you know exactly what is being put into it, you will be able to find out what the outcome will be, but the computer will do it alot faster than you and thats why it looks random.
  #13  
Old 11-01-2005
Vigilante's Avatar
TechSpot Paladin
 
Location: Arizona, USA
Member since: Dec 2004, 2,120 posts
Is this thread's author going to give some feedback?

Chances are good he's already finished the program, distributed it, and you can find it at your local Walmart. lol

Either that or Microsoft saw the monetary advantages of owning an app that makes random numbers, and so they bought him out and made sure he never programs again, thus why he can't comment here any more.

Closed Thread

Similar Topics
Topic Replies Forum
please help me i cant write numbers 7 Windows OS
Confusing Ram Numbers 2 Other Hardware
Need to change IRQ numbers 4 Windows OS
IP Numbers 6 Software Apps

Thread Tools Search this Thread
Search this Thread:

Advanced Search
All times are GMT -4. The time now is 11:37 AM.