Thread: Ask about random number

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    68

    Ask about random number

    I want to generate random number between 1- 400,000 which is not the same value.

    Example.
    If X1 = 254899
    X2 not have the same value as X1.
    and X3 not have the same value as X1 and X2 .

    I want to generate data X1 - X400,000 which not have the same value and each data have value between 1-400,000

    If you know how to do that, please tell me. Thank you.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Random numbers, then just compare the new rand number with what you've already got. If its the same, get another random number.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    How long do you reckon that method would be good for Hammer? If you wanted a number between 0 and 100 but no duplicates, when does it take to long to reach the next number because you're throwing away duplicates?
    Couldn't think of anything interesting, cool or funny - sorry.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >How long do you reckon that method would be good for Hammer?
    Yeah, I suppose I didn't read the original question fully In answer to your question, not long, I suppose. My theory is good in the short term only. There must be a better way, but it's not coming to me at present...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537

    Wink

    Depends on the situation really. If you want 100 different numbers between 1 and 1000000, then it'll be fine. But if you want 80 random numbers between 1 and 100 a better method would need be to eliminate the 20 random numbers your not going to use. Maybe...
    Couldn't think of anything interesting, cool or funny - sorry.

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I learned this with a program I wrote that shuffled cards.
    Code:
    int array[400000];
    for (int i=1; i<=400000; i++)
    {
      array[i]=i;  // first set each number equal to itself
    }
    
    for (int i=1; i<=400000; i++)
    {
       int x=rand()%400000+1;  // pick a random number between 1-400000
       // now switch this variable with the variable at the random number generated
       int temp=array[x];
       array[x]=array[i];
       array[i]=temp;
    }
    This will randomly place the numbers 1-400000 in to 400000 variables without duplicates

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    on computers its unfortuneate but some things have limits. One of those is the maximum number an int can hold.Also in the example shown RAND_MAX has surely been exceeded too. remember that rand() can only produce values up to RAND_MAX which on my system is 0x7fff
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. random number tutorial
    By 7stud in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 02:41 PM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM