Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ** Random Number Generator **

** Random Number Generator **

Scheduled Pinned Locked Moved C / C++ / MFC
questiondatabasetutoriallounge
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Steve Lai
    wrote on last edited by
    #1

    Hi Everyone, I have a question regarding generating random number. Is there a function that can generate random number between a range without having the random number repeat itself? For example, let's say I have 100 records in a database. i want to randomly select a certain number of records upon user input and make it as a new table. if i use rand() function, and mod the numbers by 100, i'll get the numbers between 1 and 100, which then i can use to set the position of the record to copy that row of data. but the numbers might repeat themself if i select a large amount of records out of the 100. is there a way to check the random number generated to see if it has already been selected before? If anyone knows, plmk. thanks steve

    E W 2 Replies Last reply
    0
    • S Steve Lai

      Hi Everyone, I have a question regarding generating random number. Is there a function that can generate random number between a range without having the random number repeat itself? For example, let's say I have 100 records in a database. i want to randomly select a certain number of records upon user input and make it as a new table. if i use rand() function, and mod the numbers by 100, i'll get the numbers between 1 and 100, which then i can use to set the position of the record to copy that row of data. but the numbers might repeat themself if i select a large amount of records out of the 100. is there a way to check the random number generated to see if it has already been selected before? If anyone knows, plmk. thanks steve

      E Offline
      E Offline
      Erik Funkenbusch
      wrote on last edited by
      #2

      == false) { // do your code gen[x] = true; } else { // try again }

      1 Reply Last reply
      0
      • S Steve Lai

        Hi Everyone, I have a question regarding generating random number. Is there a function that can generate random number between a range without having the random number repeat itself? For example, let's say I have 100 records in a database. i want to randomly select a certain number of records upon user input and make it as a new table. if i use rand() function, and mod the numbers by 100, i'll get the numbers between 1 and 100, which then i can use to set the position of the record to copy that row of data. but the numbers might repeat themself if i select a large amount of records out of the 100. is there a way to check the random number generated to see if it has already been selected before? If anyone knows, plmk. thanks steve

        W Offline
        W Offline
        WoR
        wrote on last edited by
        #3

        Hi Steve, A standard procedure for this problem is to generate a sequence 1..n and pemutate these numbers randomly (n log n times should do the job). This is efficient if you want to access a certain number of items mutliple times. Wolfgang

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups