1. TechSpot is dedicated to computer enthusiasts and power users. Ask a question and give support. Join the community here.
    TechSpot is dedicated to computer enthusiasts and power users.
    Ask a question and give support.
    Join the community here, it only takes a minute.
    Dismiss Notice

Programming help - random numbers

By tomyas ยท 12 replies
Oct 22, 2005
  1. 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,

  2. zephead

    zephead TechSpot Paladin Posts: 1,569

    what language?

    PS > do yourself a favor and remove your email address from above post, this is a public webpage.
  3. RealBlackStuff

    RealBlackStuff TS Rookie Posts: 6,503

  4. Vigilante

    Vigilante TechSpot Paladin Posts: 1,666

    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. Nodsu

    Nodsu TS Rookie Posts: 5,837   +6

    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. jobeard

    jobeard TS Ambassador Posts: 10,432   +801

    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. zephead

    zephead TechSpot Paladin Posts: 1,569

    i think he wants to do integers here...
  8. Spike

    Spike TS Evangelist Posts: 2,168

    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. Vigilante

    Vigilante TechSpot Paladin Posts: 1,666

    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:

    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. Nodsu

    Nodsu TS Rookie Posts: 5,837   +6

    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. stepfour

    stepfour TS Rookie

    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
    'this means it was previously generated
    'generate again
    end if

    Happy Programming...
  12. Pablo

    Pablo TS Rookie Posts: 16

    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. Vigilante

    Vigilante TechSpot Paladin Posts: 1,666

    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.

    :) ;)
Topic Status:
Not open for further replies.

Similar Topics

Add New Comment

You need to be a member to leave a comment. Join thousands of tech enthusiasts and participate.
TechSpot Account You may also...