Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Oct 10th, 2008, 1:43 AM   #1
strikefred0m
Newbie
 
Join Date: Oct 2008
Posts: 2
Rep Power: 0 strikefred0m is on a distinguished road
Lightbulb Repeated Random Number

Hi there, I am new there, and I searched before about my question, unfortunately I am unable to understand them and I post again. Sorry if repeating the same question.

My question is like this, ask the user to key in Max and Min as the random number range, and the random number should be in the range. All work fine but I am unable to prevent some percentage that the random generator will generate the same number. (I set the range between Max and Min must be at least same or greater than 10).

Max = top.
Min = low.
top - low >= 10.

The random generator will then generate 6 numbers in an array.

c++ Syntax (Toggle Plain Text)
  1. for (index = 0; index < 6; index++)
  2. {
  3. pcvalues[index] = (rand() + time (0)) % top ;
  4. if (pcvalues[index] < low)
  5. {
  6. do
  7. {
  8. pcvalues[index] = (pcvalues[index] + low ) % top;
  9. } while (pcvalues[index] < low);
  10. }
  11.  
  12. }

Can anyone tell me how to prevent same number repetition?

Thank a lot.

Last edited by big_k105; Oct 16th, 2008 at 12:21 PM. Reason: Changed quote tag to code tag
strikefred0m is offline   Reply With Quote
Old Oct 10th, 2008, 2:13 AM   #2
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Repeated Random Number

This thread has appeared many times in this forum. One example is linked to at the bottom of this page under "similar threads". http://www.programmingforums.org/thread12570.html

Here is another example that was just posted last week: http://www.programmingforums.org/showthread.php?t=16612

You said you searched but "don't understand them"? What don't you understand about the code? Or is your question something slightly different?
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Old Oct 10th, 2008, 3:15 AM   #3
strikefred0m
Newbie
 
Join Date: Oct 2008
Posts: 2
Rep Power: 0 strikefred0m is on a distinguished road
Re: Repeated Random Number

http://www.programmingforums.org/showthread.php?t=16612

The thread was afterward goes into Java thing isn't? I am just newbie to C++ and currently only been tough until array...

http://www.programmingforums.org/thread12570.html

for(i=0;i<100;++i)
a[i] = i+1;

for(i=0;i<100;++i)
swap(a[i], a[rand()%100]);

Sorry I can't link it to my question, can you please give an short explanation? Thank for your kind help.
strikefred0m is offline   Reply With Quote
Old Oct 15th, 2008, 5:52 AM   #4
devoox
Newbie
 
Join Date: Sep 2008
Posts: 3
Rep Power: 0 devoox is on a distinguished road
Re: Repeated Random Number

Well, if you read the whole threads, but not just checking the question, you would have found the "link" to your question - srand() function
devoox is offline   Reply With Quote
Old Oct 16th, 2008, 11:26 AM   #5
RareDevil
<html>#include C++<?php?>
 
RareDevil's Avatar
 
Join Date: Oct 2008
Location: Denmark
Posts: 14
Rep Power: 0 RareDevil is on a distinguished road
Send a message via MSN to RareDevil
Re: Repeated Random Number

If you want to have fun with random numbers then try to look at this code. It was one of the first i have made.

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. int outPut();
  9. int inPut();
  10. int textOut();
  11.  
  12. int main()
  13. {
  14. srand( (unsigned) time(0) );
  15. int tempRand;
  16. int even = 0;
  17. int temp;
  18. long int sample = 1000000000;
  19. ofstream my_stream("outFile.txt", ios::trunc);
  20. ifstream in_stream;
  21. for (int i = 0; i < sample; i++)
  22. {
  23. tempRand = (rand()%9999) + 1;
  24. my_stream << tempRand << endl;
  25. }
  26. my_stream.close();
  27. in_stream.open("outFile.txt");
  28. while ( in_stream )
  29. {
  30. in_stream >> temp;
  31. if ( (temp % 2) == 0 )
  32. even++;
  33. }
  34. cout << "Total Numbers = " << sample << endl
  35. << "Even: " << even << endl
  36. << "Odd: " << sample - even << endl;
  37. system("pause");
  38. return 0;
  39. }

You maybe need to change "long int sample = 1000000000;" to somethign like "long int sample = 1000000;" or a less number else will this little program make a 5GB txt file
RareDevil 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
non-repetitive random number malms C 4 Feb 14th, 2007 1:17 PM
Random Number coldDeath Python 1 Sep 11th, 2005 9:17 AM
Random Number & Average Problem Hadrurus Java 6 Aug 15th, 2005 2:08 PM
generate a random number cwl157 Java 3 Apr 15th, 2005 7:18 PM
non repeating random number generation gencor45 C# 2 Feb 9th, 2005 1:11 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:17 AM.

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