Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 14th, 2007, 1:30 AM   #1
malms
Newbie
 
Join Date: Feb 2007
Posts: 1
Rep Power: 0 malms is on a distinguished road
non-repetitive random number

hi guys,
when i generate k random numbers in range from 1 to 100, usually i got a few numbers that repeat themselves.
how can i generate the k non-repetitive random number?
i tried to do error checking, whether the new generated random number is the same with the old one, but it's not working

thanks
malms is offline   Reply With Quote
Old Feb 14th, 2007, 4:00 AM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
Aside from using a method which will incur some overhead, you can't. And if it's random, you should allow for the possibility of having a duplicate. You could keep a list of previous numbers and check for a duplicate if you wanted to. Or, you could keep a pool of valid numbers, shuffle it up, and remove randomly indexed values from the shuffled list.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Feb 14th, 2007, 7:54 AM   #3
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
or you can do something like...
  for(i=0;i<100;++i)
     a[i] = i+1;

  for(i=0;i<100;++i)
     swap(a[i], a[rand()%100]);
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Feb 14th, 2007, 9:52 AM   #4
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3 kruptof is on a distinguished road
Quote:
Originally Posted by InfoGeek View Post
or you can do something like...
  for(i=0;i<100;++i)
     a[i] = i+1;

  for(i=0;i<100;++i)
     swap(a[i], a[rand()%100]);
you should aslo give srand a call before calling rand or everytime you start the program you will get the same random numbers as you got last time you ran the program
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Feb 14th, 2007, 12:17 PM   #5
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
If you are dealing a deck of cards, infogeek's method is what you want. You can deal a card only once. If you are doing something with "random" selections, then repeats have to be allowed, because that is what random means.
jim mcnamara is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB 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
Median/Mode in arrays? {Need help} Java|Tera Java 27 Nov 29th, 2005 10:50 AM
FiveDigit + RandomeNumber Game. TecBrain Java 0 Nov 18th, 2005 2:53 PM
Random Number coldDeath Python 1 Sep 11th, 2005 8:17 AM
Random Number & Average Problem Hadrurus Java 6 Aug 15th, 2005 1:08 PM
non repeating random number generation gencor45 C# 2 Feb 9th, 2005 12:11 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:38 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC